Reputation: 1286
I have several tables:
Quote
id_quote (PK)
ItemQuote
id_item_quote (PK)
quote_id (FK)
item_id (FK)
Item
item_id (PK)
On each ItemQuote, there can be several Services:
ItemQuoteService
id_item_quote_service (PK)
item_quote_id (FK)
service_id (FK)
Service
id_service (PK)
I use two custom pivot entities, because there is other fields in the relation:
class Quote extends Model {
// ...
public function items() {
return $this->belongsToMany(Item::class, 'ItemQuote', 'quote_id')
->using(ItemQuote::class);
}
}
class ItemQuote extends Model {
// ...
public function services() {
return $this->belongsToMany(Service::class, 'ItemQuoteService', 'id_item_quote')
->using(ItemQuoteService::class);
}
}
When I'm getting a Quote, I can easily aces ItemQuote
$quote = Quote::with([
'items',
])->findOrFail($id);
$quote->items->pivot // contains the attributes of ItemQuote
I want to also have access to the attributes of ItemQuoteService. But I don't know what to pass to the with
function to make it work.
$quote = Quote::with([
'items.pivot.service', // NOT WORKING
])->findOrFail($id);
How can I acces Quote->ItemQuote->ItemQuoteService
attributes (from the fetching of one Quote) ?
Upvotes: 0
Views: 28
Reputation: 7561
You might be able to retrieve them using an extra relation:
// Class Quote
public function ItemQuoteServices()
{
return $this-hasManyThrough(ItemQuoteService::class, ItemQuote::class, 'quote_id', 'item_quote_id');
}
You then call it using $quote->with(['itemQuoteServices']);
.
You have to double check those 3rd and 4th parameters, it might just work if you leave them out.
You could also create an ItemQuoteServices()
on the Item
class (much the same code), but then you have to call it using ->with(['items', 'items.itemQuoteServices'])
, which retrieves a bit more data.
Upvotes: 1