Has anyone had success using the Group filter with Squarebit Charge?

I tried a few different approaches, in each case getting array errors. Here is code:

{% set allCharges = craft.charge.charges.limit(3)|group('cardName') %}

{% for cardName, details in allCharges %}

{{ cardName }}

{% for details in allCharges %} 
    {{ details.dateCreated }}...and more fields
{% endfor %}

{% endfor %}

This throws an error message "Key "dateCreated" for array with keys "0" does not exist."

If I dump(details) I see an array that looks like all the fields you would expect, including "dateCreated"

Any help would be appreciated!

  • Is there a certain reason why you loop twice? Jan 6 '18 at 0:14

I got this to work with the following:

{% set allCharges = craft.charge.charges.limit(5) %}
{% set charges = allCharges|group("dateCreated|date('F')") %}

{% for month, details in charges %}
        {{ month }}

        {% for detail in details %}
        {% endfor %}
{% endfor %}

Two things to note: the group filter was applied to the results of the Charges query, rather than applying it to the query itself; the second issue is that the Charge model contains sub-models,which will require an inner loop.

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.