1

I'm looking to eager load and set another variable within the eager loading. But I can't seem to find out how on numerous guides/tutorials, and the query seems to be slowing the system down a bit.

  {% set entries = craft.entries ({
          section: 'events',
          order: 'postDate desc',
          with: ['eventMainImage'],[''],
          relatedTo: category
      }) %}

  {% for entry in entries %}
    --> {% set venue = entry.eventVenueId.one() %}

As you can see by the arrow, I'm wanting to set this property, is this possible within an eager load?

2

Eager-Loading is not like a hook or a callback. You wouldn't set variables within it. You'd want to use eager-loading to load your eventVenueId then in your loop you'd set the variable a little differently.

 {% set entries = craft.entries ({
          section: 'events',
          order: 'postDate desc',
          with: ['eventMainImage', 'eventVenueId'],
          relatedTo: category
  }) %}

 {% for entry in entries %}
    {% set venue = entry.eventVenueId[0] ?? null %}

Additionally you could look at eager-loading a Nested Set of Elements to further optimize your query if possible with the data you need off your eventVenueId elements.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.