<p>Without eager-loading, accessing the categories field will return a <a href="https://docs.craftcms.com/api/v4/craft-elements-db-categoryquery.html" rel="nofollow noreferrer"><code>CategoryQuery</code></a> which you can add parameters to and execute with methods like <code>exists()</code>. With eager-loading, the categories are already loaded before you access them, so instead of a query you get an <a href="https://docs.craftcms.com/api/v4/craft-elements-elementcollection.html" rel="nofollow noreferrer"><code>ElementCollection</code></a>.</p>
<p>The element collection has some custom methods to be compatible with element queries, and extends <code>Illuminate\Support\Collection</code>, so you can use all the various <a href="https://laravel.com/docs/9.x/collections" rel="nofollow noreferrer">collection methods</a>.</p>
<p>You can check if the collection contains a collection with a specific ID like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% if product.NameOfproductCategory.contains('id', '9999') %}
</code></pre>

CC BY-SA 4.0
2023-07-21T07:45:54.073Z
Thanks! One thing though, it's not `.contains`, but `|contains`. Can you adjust your answer, i cannot.
Henrik
CC BY-SA 4.0
2023-07-21T09:02:07.677Z
@Henrik No, `.contains` is correct. As I said, since you have a [collection](https://laravel.com/docs/9.x/collections), you can call any of it's instance methods. In this case, I'm calling the [`.contains` method](https://laravel.com/docs/9.x/collections#method-contains). You can also use `|contains`, which is the Craft filter of the same name.
MoritzLost
CC BY-SA 4.0
2023-07-24T05:12:07.103Z
Ah, ok. `.contains` did not work in my case, but `|contains` did. That's why i thought you meant maybe the filter.
Henrik
CC BY-SA 4.0
2023-07-24T08:31:23.920Z
@Henrik If `.contains` doesn't work, that means your categories were not eager-loaded! You can check whether the categories field gives you can `ElementCollection` or a `CategoryQuery` like this: `{% dd product.NameOfProductCategory %}`. If you get an `CategoryQuery`, that means your eager-loading is not working properly. But yes, `|contains` should work with arrays as well as collections, so it's safe to use if you're not sure if the field is eager-loaded or not. If your question is solved, don't forget to mark the answer as accepted.
MoritzLost
CC BY-SA 4.0
2023-07-24T08:53:12.847Z
thanks! interessting. I used the blitz recommendations to speed up my a site and after i inserted the code in my question, the hint to eager-load my categories are gone and i noticed a huge decrease of db request in the debug toolbar. so i thought i successfully eager-loaded. have to look to that again then.
Henrik
CC BY-SA 4.0
2023-07-20T13:30:33.357Z
2023-07-20T13:30:33.357Z
MoritzLost
<p>On my craft commerce project i eager-load categories which belong to a prodcut.</p>
<p>in my product template i do for eager -loading:</p>
<pre><code>{% do craft.app.elements.eagerLoadElements(
  className(product),
  [product],
  [
    ['NameOfproductCategory'],
  ]
) %}
</code></pre>
<p>Before i added this eager-loading i checked if a certain categroy exists in a product to change the sytling:</p>
<pre><code>{% if product.NameOfproductCategory.id('9999').exists() %}
    &lt;div class=&quot;newproduct&quot;&gt;
    &lt;/div&gt;
{% endif %}
</code></pre>
<p>now with this eager-loading i get this error and i dont know how to solve it.</p>
<pre><code>Impossible to invoke a method (&quot;id&quot;) on an array.
</code></pre>

<categories><eager-loading>
Check if eager-loaded category exists on product
# Check if eager-loaded category exists on product

(Asked by: username_0 on 2023-07-20)

On my craft commerce project i eager-load categories which belong to a prodcut.
in my product template i do for eager -loading:
{% do craft.app.elements.eagerLoadElements(
  className(product),
  [product],
  [
    ['NameOfproductCategory'],
  ]
) %}

Before i added this eager-loading i checked if a certain categroy exists in a product to change the sytling:
{% if product.NameOfproductCategory.id('9999').exists() %}
    
    
{% endif %}

now with this eager-loading i get this error and i dont know how to solve it.
Impossible to invoke a method ("id") on an array.

**Tags:** categories, eager-loading

---

## 1 answers

### Answer by username_1 on 2023-07-20. Score: 1 | Accepted answer

Without eager-loading, accessing the categories field will return a CategoryQuery which you can add parameters to and execute with methods like exists(). With eager-loading, the categories are already loaded before you access them, so instead of a query you get an ElementCollection.
The element collection has some custom methods to be compatible with element queries, and extends Illuminate\Support\Collection, so you can use all the various collection methods.
You can check if the collection contains a collection with a specific ID like this:
{% if product.NameOfproductCategory.contains('id', '9999') %}

> **Comments:**
> **username_0**: Thanks! One thing though, it's not `.contains`, but `|contains`. Can you adjust your answer, i cannot.  
> **username_1**: @Henrik No, `.contains` is correct. As I said, since you have a [collection](https://laravel.com/docs/9.x/collections), you can call any of it's instance methods. In this case, I'm calling the [`.contains` method](https://laravel.com/docs/9.x/collections#method-contains). You can also use `|contains`, which is the Craft filter of the same name.  
> **username_0**: Ah, ok. `.contains` did not work in my case, but `|contains` did. That's why i thought you meant maybe the filter.  
> **username_1**: @Henrik If `.contains` doesn't work, that means your categories were not eager-loaded! You can check whether the categories field gives you can `ElementCollection` or a `CategoryQuery` like this: `{% dd product.NameOfProductCategory %}`. If you get an `CategoryQuery`, that means your eager-loading is not working properly. But yes, `|contains` should work with arrays as well as collections, so it's safe to use if you're not sure if the field is eager-loaded or not. If your question is solved, don't forget to mark the answer as accepted.  
> **username_0**: thanks! interessting. I used the blitz recommendations to speed up my a site and after i inserted the code in my question, the hint to eager-load my categories are gone and i noticed a huge decrease of db request in the debug toolbar. so i thought i successfully eager-loaded. have to look to that again then.  

---

