laravel-relation-joins icon indicating copy to clipboard operation
laravel-relation-joins copied to clipboard

Eager Load relationship models from join queries

Open Shadercloud opened this issue 4 months ago • 0 comments

Maybe I'm doing something wrong, but if I already joining the relationships why should eloquent have to do another query to fetch the relationships, the data is already available within the join?

For example:

Card::with(['set.sport'])->joinRelation('set.sport')->where('person_id', 10);

Results in these 3 queries:

select * from `cards` inner join `sets` on `sets`.`id` = `cards`.`set_id` inner join `sports` on `sports`.`id` = `sets`.`sport_id` where `cards`.`person_id` = 10
select * from `sets` where `sets`.`id` in (2, 3, 5)
select * from `sports` where `sports`.`id` in (7)

If i remove the with() then it just queries each card relationship individually and results in 100s of queries. How can I get this down to just 1 query?

Shadercloud avatar Sep 27 '24 10:09 Shadercloud