Skip to content

How to eager load relations? #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
maxi032 opened this issue Feb 3, 2023 · 8 comments
Open

How to eager load relations? #119

maxi032 opened this issue Feb 3, 2023 · 8 comments
Labels
wontfix This will not be worked on

Comments

@maxi032
Copy link

maxi032 commented Feb 3, 2023

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

@ryanmitchell
Copy link
Contributor

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

See:

public function with($relations, $callback = null)

@maxi032
Copy link
Author

maxi032 commented Feb 3, 2023

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.

@ryanmitchell ryanmitchell added the wontfix This will not be worked on label Jun 14, 2023
@de-raaf-media
Copy link

I am experiencing large performance issues with taxonomies. I now see that "with" is not implemented. This is not going to be implemented?

@ryanmitchell
Copy link
Contributor

There are no plans to implement it.

@adampatterson
Copy link
Contributor

Would it mess things up if we stored the taxonomy IDs with the entry instead of handles?

I think that would allow for a taxonomy relationship to the Entry model.

@ryanmitchell
Copy link
Contributor

Not sure I’m following what you mean?

@adampatterson
Copy link
Contributor

Preface: I'm still working on my first Statamic site, so I don't have a super great grasp of the inner workings yet. 😄

If you create your own models there's nothing preventing you from adding relations on the Entry model.

Would it be possible then to create a taxonomy relationship and I guess a pivot if require. Rather than storing taxonomies in the entry by handle you could grab them by ID.

public function categories()
    {
        $categoryIds = $this->data['categories'] ?? [];

        return Taxonomy::whereIn('id', $categoryIds)->get();
    }

I think this approach would end up being situational though because the data column doesn't know that categories is a taxonomy without referencing some other tables.

@ryanmitchell
Copy link
Contributor

ryanmitchell commented Mar 8, 2025

A term ID is just the slug/handle prefixed with the taxonomy handle. So you could do this as is.

Longer term I'm hoping some changes in core will allow this driver to use proper pivot tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants