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

brjig's avatar
Level 2

Relationship inside a pivot table using the pivot table id

Okay, im getting stumped here. I have seen threads here with answers, but im still stumped and keep getting errors.

I have 4 tables

  • stores (extends model)
id
store_name
  • products (extends model)
id
title
  • product_store (extends pivot)
id
product_id
store_id
timestamps
  • inventories (extends model)
id
product_store_id
quantity

Here is the model setup

Store extends Model 

public function products {
return $this->belongsToMany(Product::class, 'product_store')
            ->using(ProductStore::class)
             ->as('store_product')
            ->withTimestamps();
    }
}
Product extends Model 

public function stores {
return $this->belongsToMany(Store::class, 'product_store')
            ->using(ProductStore::class)
             ->as('store_product')
            ->withTimestamps();
    }
}
ProductStore extends pivot

public function inventory()
    {
        return $this->hasOne(Inventory::class, 'product_store_id', 'id');
    }

Here is the issue, when loading the store and all its products using the pivot table, how can i also include the inventory inside the pivot

I know this works

$store->products()->first()->store_product->load('inventory');

however this isnt ideal as its a n+1 issue since i need to call it each time i loop through the products

I have been trying different versions of the load/with method, but each variation throws some sort of error I feel like the solution is simple, but i have been looking at this far too long to admit.

return $store->with(['products', 'products.inventory])   == error  undefined relationship on model inventory on model product

return $store->with(['products', 'products.pivot.inventory'])   == error

return $store->with(['products', 'products.product_store.inventory'])   == error

Techincally I believe the response should look like

id: 1,
store_name: 'cool store',
products: [
   0: [
      id: 1,
      title: 'product title,
      store_product: [
         id: 1,
         product_id: 0,
         store_id: 1,
         inventory: [
              id: 1,
              store_product_id: 1,
              quantity: 10,
         ]
      ]
   ],
   1: [...],
   2: [...],
   3: [...]
]

Any direction and help is appreciated

I know i can add the inventory data directly on the pivot table itself, but the reason i split it is because the api call too pull the the inventory data is a separate call than the products, so i can just update the inventory table itself without worrying about the the other tables, just like the api call for the store products is separate and i can just update the product_store table seperatly

0 likes
1 reply
mabdullahsari's avatar

You can't eager load pivot specific relations with the default Eloquent builder. I think I read about a package on Laravel News some time ago that enabled this behavior.

Please or to participate in this conversation.