eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

How to eager load relations?

Open maxi032 opened this issue 2 years ago • 4 comments

I try to load a page and the associated taxonomies terms of it but the problem is that extra queries are being done. I am using Statamic multisite. I would like to be able to eager load only those associated with that specific entry. So I have a tags taxonomy and I want something like:

$queryBuilder = Entry::with(['tags'])->query()
->where('collection','pages')
->where('slug', 'my-page')
->where('site', 'en');
->where('published', true)->get();

Many thanks

maxi032 avatar Feb 03 '23 11:02 maxi032

At the moment with() doesn't do anything in the Eloquent Entry Query builder.

See: https://github.com/statamic/eloquent-driver/blob/83d461614d1967036e0d0ec1dd069e3ce3d82e42/src/Entries/EntryQueryBuilder.php#L84

ryanmitchell avatar Feb 03 '23 13:02 ryanmitchell

I thought so... this takes away one of the greatest advantages of using Laravel, the fact that I can load related content basically on the fly.

maxi032 avatar Feb 03 '23 13:02 maxi032