Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-21T07:45:54.073Z
Answers.Text: Thanks! One thing though, it's not `.contains`, but `|contains`. Can you adjust your answer, i cannot.
Answers.UserDisplayName: Henrik
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-21T09:02:07.677Z
Answers.Text: @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.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-24T05:12:07.103Z
Answers.Text: Ah, ok. `.contains` did not work in my case, but `|contains` did. That's why i thought you meant maybe the filter.
Answers.UserDisplayName: Henrik
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-24T08:31:23.920Z
Answers.Text: @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.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-24T08:53:12.847Z
Answers.Text: 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.
Answers.UserDisplayName: Henrik
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-20T13:30:33.357Z
Answers.LastActivityDate: 2023-07-20T13:30:33.357Z
Answers.UserDisplayName: MoritzLost
Body: <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>
  
Tags: <categories><eager-loading>
Title: Check if eager-loaded category exists on product
ThreadText: # 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() %}
      <div class="newproduct">
      </div>
  {% 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`](https://docs.craftcms.com/api/v4/craft-elements-db-categoryquery.html) 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`](https://docs.craftcms.com/api/v4/craft-elements-elementcollection.html).
  
  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](https://laravel.com/docs/9.x/collections).
  
  You can check if the collection contains a collection with a specific ID like this:
  
  ```twig
  {% 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**: @username_0 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**: @username_0 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.  
  
  ---
  
  