Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eminos's avatar
Level 17

Append pivot data to Model (?)

I'm stuck on something that I think should be simple to solve!

I have two models: ProductCategory and Attribute, which are connected with belongsToMany (and thus a pivot table).

Now I'm fetching a collection of Attributes:

Attribute::whereHas('productCategories', function ($query) {
        $query->where('product_category_id', $this->id);
})->get()

This works fine, BUT, I'd like to attach some pivot data to those Attributes. They are already in the DB, I just need to "append" them.

How do I append pivot data to the Attribute model?

0 likes
2 replies
neilherbertuk's avatar

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

paulomunky's avatar

Hi Eminos,

I've not tested the code below, but I think you should be able to do something like this:

$attributes = Attribute::whereHas('productCategories', function ($query) { $query->where('product_category_id', $this->id); });

// Now eager load the product categories including any pivot data, eg.

$attributes = $attributes->with('productCategories')->get();

This should append the productCategories data to the collection (within a 'product_categories' attribute) - any pivot data should now be available via the associated 'pivot' attribute.

I am assuming you have your relationship set up something like this:

class Attribute extends Model {

public function productCategories() {
    return $this->belongsToMany('Models\ProductCategory', 'attribute_categories, 'attribute_id', 'product_category_id')->withPivot('x', 'y', 'z');

}

(x,y,z represents any pivot data you have on your join table)

Hope this helps, Paul

Please or to participate in this conversation.