This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

coycash12497
coycash12497

I have a Configuration and a Part class which are related via a pivot table with BelongsToMany relationships. My backend is working perfectly adding Parts to Configurations. However on the front-end I can't get the parts listed for a given configuration. For example if I output

{{ config }}

on a component I will get

{"id":1,"slug":"economy-rig","title":"Economy Rig","subtitle":"An economic price point","created_at":"2017-12-07 22:42:15","updated_at":"2017-12-07 22:42:15","parts":[{"id":6,"name":"ASUS Mobo 565","description":"ASUS Motherboard for Intel i-series","url":null,"part_category_id":1,"created_at":"2017-12-07 22:42:17","updated_at":"2017-12-07 22:42:17","item_id":null,"id_type":null,"pivot":{"configuration_id":1,"part_id":6}}]} 

But if I try

{{ config.parts }}

I will get nothing.

Here is my Configuration class:

class Configuration extends Model
{

public $fillable = [
    'title', 'subtitle'
];

public $slugs = ['slug' => 'title'];

    public $table = 'webtank_builds_configurations';

public $belongsToMany = [
    'parts' => [
        'Webtank\Builds\Models\Part',
        'table' => 'webtank_builds_configs_parts',
        'order' => 'name'
    ],
    'parts_count' => [
        'Webtank\Builds\Models\Part',
        'table' => 'webtank_builds_configs_parts',
        'count' => true
    ]
];
}

Here is my Part class:

class Part extends Model
{
public $table = 'webtank_builds_parts';

protected $fillable = [
    'name', 'description'
];

public $belongsTo = [
    'partCategory'  => ['Webtank\Builds\Models\PartCategory'],
];

public $belongsToMany = [
    'configurations' => [
        'Webtank\Builds\Models\Configuration',
        'table' => 'webtank_builds_configs_parts',
        'order' => 'name'
    ]
];
}

and on a component from my initial example I have

public function onRun() {
    $this->page['config'] = Configuration::where('slug', $slug)->with('parts')->first();
}

I've been struggling with this for days if anyone has any ideas. Thanks

coycash12497
coycash12497

I've finally found my problem, I have a left over field on my database migration called 'parts.' Leftover from when I was using it as a 'jsonable' field. Just had to remove that. Thanks

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.