Hi Eminos,
Not sure you are using Pivot or Many to Many relationships in the right way, it's hard to tell with what code you've given. Check out https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
This is how I would do this
Attribute.php model
public function productCategories()
{
return $this->belongsToMany('App\ProductCatagory');
}
ProductCategory.php model
public function attributes()
{
return $this->belongsToMany('App\Attribute');
}
You would now have a new table on your database named Attribute_ProductCatagory with an id field for both tables. Attribute_id & ProductCategory_id. Laravel will automatically try to use this table and the id fields to perform lookups.
You would then do something that's the reverse of what you are currently using to get attributes. You would do this to get the attributes as part of your product category lookup.
$productCategory = ProductCategory::where(...what ever logic you want here..)->with('attributes')->first();
Attributes would then be available via $productCategory->attributes
To attach an attribute, you can use the attach method.
$productCategory->attributes()->attach('attribute id here');
This would create the entry on the pivot table for you. You would do the exact same with the attribute to attach a product category.
Neil