D
D
Dmitry Evgrafovich2015-09-21 18:56:46
Laravel
Dmitry Evgrafovich, 2015-09-21 18:56:46

Is it possible to eager load a pivot table?

There are two models, User and Car. There is a linking table car_user, which contains additional fields of type location_id. Now the name of the location is obtained through $car->pivot->location->name (the $cars array is $user->cars), but it is not possible to make a greedy request using eloquent to get all locations, so a lot of requests are created when accessing location. How to implement eager loading in this pivot and has anyone done it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Akhmadullin, 2015-09-21
@Tantacula

On the Laravel forum, they suggest creating a regular model for pivot tables and linking all three models to each other with the necessary types of links, and then eager loading between them. Something like this)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question