1

In Craft 2 I used:

{% for image in entry.imageGallery.offset(1) %}

to exclude the first image, as that is shown higher up in the template. I get the depreciation error of:

Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.

So I changed it to:

{% for image in entry.imageGallery.all() %}

But I can't find any example of how to apply an 'offset' in the Craft 3 docs.

3
{% for image in entry.imageGallery.offset(1).all() %}

Don't search for an example "how to apply offset". All properties work the same - there is nothing special/ no difference between offset, limit, a field value or anything else.

  • I had tried {% for image in entry.imageGallery.all().offset(1) %} and got a twig runtime error of "Impossible to invoke a method ("offset") on an array." I thought I had tried it both ways but obviously hadn't. But not being able to find any example or mention of using offset made me wonder if it had been dropped. I'm finding so many minor changes required for Craft 3 that are not obvious to me how to update my old code. I spent hours yesterday trying to update a conditional query comparing 2 dates. It doesn't help that SE doesn't separate between Craft 2 and 3. – Paul Frost Aug 13 '18 at 19:40
  • Feel free to join slack so you can ask for those scenarios much faster. Besides that Andrew's answer is really good in terms of understanding what you actually do. All in all Craft 3 has not many differences between Craft 2. As long as you understood what you did in Craft 2 it should be easy. – Robin Schambach Aug 13 '18 at 19:43
  • I guess the problem is that sometimes I didn't really understand how it was working, I had help from people like you or copy/pasted examples from solutions posted. I can usually work out how to ammend an existing example but struggle to write code from scratch. – Paul Frost Aug 13 '18 at 19:50
2

So after reading Robin's answer, I realized I misread your question entirely... so the below isn't addressing your question exactly.

I initially deleted it, but I think it may be relevant information anyway, if you decide to eager load elements into your template, or you try to use the same query more than once.

.....

When you have something like entry.imageGallery you actually have an ElementQuery -- when you invoke the .all() method on that query, you're returned and array of results... so entry.imageGallery then changes from being an ElementQuery to a simple array.

That's why you're getting the error message that you are; you're trying to invoke an ElementQuery on what is now an array.

So you have a few options here, but I think the best option is to just realize that once you've retrieved these things from the ElementQuery (or even better, have Eager Loaded them), they are now an array.

So let's treat them like an array, and modify our loop use the Twig slice filter to use just the items we want:

{% for image in entry.imageGallery | slice(1) %}

...or you can use the shorthand notation:

{% for image in entry.imageGallery[1:] %}
  • If I use the slice filter as you suggest it works but I get a depreciation error of: "Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them." – Paul Frost Aug 13 '18 at 19:46
  • I was assuming that you had already executed that ElementQuery somewhere, or the elements had been eager loaded, which would mean that they are arrays already. If not, tack .all() on the end – andrew.welch Aug 13 '18 at 23:54
  • It's an assets field on a single entry template so I thought everything was already loaded. But re-reading your Eager Loading blog post I see that I need to pre-load the matrix fields as a separate process. This is an example of where I find Craft less than easy to understand and requiring lots of code for what seems like a simple template. – Paul Frost Aug 14 '18 at 9:07

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.