Skip to content

How to eager load relations? #119

Open

Description

@maxi032

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

Activity

  1. ryanmitchell commented on Feb 3, 2023

    @ryanmitchell
    Collaborator

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

    See:

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

  2. maxi032 commented on Feb 3, 2023

    @maxi032
    Author

    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.

  3. added
    wontfix
    on Jun 14, 2023
  4. de-raaf-media commented on Oct 2, 2024

    @de-raaf-media

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

  5. ryanmitchell commented on Oct 2, 2024

    @ryanmitchell
    Collaborator

    There are no plans to implement it.

  6. adampatterson commented on Mar 8, 2025

    @adampatterson
    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.

  7. ryanmitchell commented on Mar 8, 2025

    @ryanmitchell
    Collaborator

    Not sure I’m following what you mean?

  8. adampatterson commented on Mar 8, 2025

    @adampatterson
    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.

  9. ryanmitchell commented on Mar 8, 2025

    @ryanmitchell
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions