Answers.Body: <p>The issue is that your <code>classes</code> variable isn't an element query when you try to add the <code>relatedTo</code> query param using <code>|merge</code> – it's an array, because you've already executed the query using <code>.all()</code>. The error occurs because your <code>classes</code> array now consists of a number of Matrix blocks, plus a regular array containing the <code>relatedTo</code> (which is where the error occurs).</p>
  <p>To fix the error, make sure to add the <a href="https://craftcms.com/docs/4.x/relations.html#the-relatedto-parameter" rel="nofollow noreferrer"><code>relatedTo</code></a> param as an actual query param, before the query is executed, for example:</p>
  <pre><code>{# Build base query #}
  {% set classesQuery = craft.matrixBlocks()
    .type('classDay')
    .fieldId('54')
    .limit('6')
    .classDate(['and', &quot;&gt;= #{startDate}&quot;])
    .orderBy('classDate ASC')
  %}
  
  {# Check for class categories #}
  {% set classCats = block.courseCategory.all() %}
  {% if classCats %}
      {% do classesQuery.relatedTo(['and'] | merge(classCats)) %}
  {% endif %}
  
  {# Execute the query #}
  {% set classes = classesQuery.all() %}
  
  {% for class in classes %}
      ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-26T17:29:46.063Z
Answers.Text: that stopped the error but now I get no results event though I've manuallly edited entries to ensure that there entries in the category I'm testing.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-26T18:00:35.933Z
Answers.Text: Hard to say why; I don't really understand the content model here. It might be due to the `['and'] | merge(classCats)` – in your original code, you had `['and', classCats]`. That syntax doesn't really make sense unless you're [combining multiple relational criteria](https://craftcms.com/docs/4.x/relations.html#compound-criteria), so I assumed the intent was that each "class" needs to have a relation to *all* the categories defined in `block.courseCategory`. If that's not the case (i.e. each "class" only needs to relate to at least 1 of the categories), you can just do `.relatedTo(classCats)`.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-25T19:07:47.770Z
Answers.LastActivityDate: 2023-07-25T19:17:55.410Z
Answers.LastEditDate: 2023-07-25T19:17:55.410Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I've got classes by entry and each week is a matrix block inside that week. I'm then outputting all weeks together using this query</p>
  <pre><code>{% set startDate = date(now)|atom %}
  
  {% set classes = craft.matrixBlocks()
    .type('classDay')
    .fieldId('54')
    .limit('6')
    .classDate(['and', &quot;&gt;= #{startDate}&quot;])
    .orderBy('classDate ASC')
    .all()
  %}
  </code></pre>
  <p>Which works fine. this is the entire for loop (simplified</p>
  <pre><code>{% for class in classes %}
  
  {# set the entry that this matrix block comes from #}
  {% set mainEntry = craft.entries.id(class.primaryOwnerId).one() %}
  
  &lt;div class=&quot;relative duration-300 bg-white shadow-xl swiper-slide hover:scale-105&quot;&gt;
    &lt;div class=&quot;relative &quot;&gt;
  
      {% set image = mainEntry.image.one() ?? null %}
      {% if image|length %}
      &lt;img src=&quot;{{ image.url }}&quot; alt=&quot;&quot;&gt;
  
      {% if mainEntry.classFormat|length %}
        some code here
      {% endif %}
    &lt;/div&gt;&lt;!-- /.relative --&gt;
    &lt;div class=&quot;p-6 mb-8&quot;&gt;
      &lt;p class=&quot;mb-2 text-base font-lato text-blueBrand&quot;&gt;
        {{ class.classDate|date('l, F d') }}
      &lt;/p&gt;&lt;!-- /.font-lato text- text-blueBrand --&gt;
      &lt;h3 class=&quot;font-merriweather text-[1.3rem]&quot;&gt;&lt;a href=&quot;{{ mainEntry.url }}&quot;&gt;{{ mainEntry.title }}&lt;/a&gt;&lt;/h3&gt;
    &lt;/div&gt;
  &lt;/div&gt;&lt;!-- /.slide --&gt;
  {% endfor %}
  </code></pre>
  <p>This is all in a matrix block of another entry where I'm outputting a slider. Now the client wants to restrict the results to a category chosen in this matrix field. I've set that up but I get errors when I add the following below the main classes query</p>
  <pre><code>{% set classCats = block.courseCategory.all() %}
  {% set classes = classes|merge({ relatedTo:['and', classCats] }) %}
  </code></pre>
  <p>The error is:</p>
  <p>Key &quot;primaryOwnerId&quot; for array with keys &quot;0, 1&quot; does not exist.</p>
  <p>and references this line of code</p>
  <pre><code>{# set the entry that this matrix block comes from #}
  {% set mainEntry = craft.entries.id(class.primaryOwnerId).one() %}
  </code></pre>
  <p>I cannot figure out how to resolve this.</p>
  
Tags: <matrix><relatedto>
Title: craft.matrixBlocks - related to entry
ThreadText: # craft.matrixBlocks - related to entry
  
  (Asked by: username_0 on 2023-07-25)
  
  I've got classes by entry and each week is a matrix block inside that week. I'm then outputting all weeks together using this query
  
  ```
  {% set startDate = date(now)|atom %}
  
  {% set classes = craft.matrixBlocks()
    .type('classDay')
    .fieldId('54')
    .limit('6')
    .classDate(['and', ">= #{startDate}"])
    .orderBy('classDate ASC')
    .all()
  %}
  
  ```
  
  Which works fine. this is the entire for loop (simplified
  
  ```
  {% for class in classes %}
  
  {# set the entry that this matrix block comes from #}
  {% set mainEntry = craft.entries.id(class.primaryOwnerId).one() %}
  
  <div class="relative duration-300 bg-white shadow-xl swiper-slide hover:scale-105">
    <div class="relative ">
  
      {% set image = mainEntry.image.one() ?? null %}
      {% if image|length %}
      <img src="{{ image.url }}" alt="">
  
      {% if mainEntry.classFormat|length %}
        some code here
      {% endif %}
    </div><!-- /.relative -->
    <div class="p-6 mb-8">
      <p class="mb-2 text-base font-lato text-blueBrand">
        {{ class.classDate|date('l, F d') }}
      </p><!-- /.font-lato text- text-blueBrand -->
      <h3 class="font-merriweather text-[1.3rem]"><a href="{{ mainEntry.url }}">{{ mainEntry.title }}</a></h3>
    </div>
  </div><!-- /.slide -->
  {% endfor %}
  
  ```
  
  This is all in a matrix block of another entry where I'm outputting a slider. Now the client wants to restrict the results to a category chosen in this matrix field. I've set that up but I get errors when I add the following below the main classes query
  
  ```
  {% set classCats = block.courseCategory.all() %}
  {% set classes = classes|merge({ relatedTo:['and', classCats] }) %}
  
  ```
  
  The error is:
  
  Key "primaryOwnerId" for array with keys "0, 1" does not exist.
  
  and references this line of code
  
  ```
  {# set the entry that this matrix block comes from #}
  {% set mainEntry = craft.entries.id(class.primaryOwnerId).one() %}
  
  ```
  
  I cannot figure out how to resolve this.
  
  **Tags:** matrix, relatedto
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-07-25. Score: 1
  
  The issue is that your `classes` variable isn't an element query when you try to add the `relatedTo` query param using `|merge` – it's an array, because you've already executed the query using `.all()`. The error occurs because your `classes` array now consists of a number of Matrix blocks, plus a regular array containing the `relatedTo` (which is where the error occurs).
  
  To fix the error, make sure to add the [`relatedTo`](https://craftcms.com/docs/4.x/relations.html#the-relatedto-parameter) param as an actual query param, before the query is executed, for example:
  
  ```
  {# Build base query #}
  {% set classesQuery = craft.matrixBlocks()
    .type('classDay')
    .fieldId('54')
    .limit('6')
    .classDate(['and', ">= #{startDate}"])
    .orderBy('classDate ASC')
  %}
  
  {# Check for class categories #}
  {% set classCats = block.courseCategory.all() %}
  {% if classCats %}
      {% do classesQuery.relatedTo(['and'] | merge(classCats)) %}
  {% endif %}
  
  {# Execute the query #}
  {% set classes = classesQuery.all() %}
  
  {% for class in classes %}
      ...
  
  ```
  
  > **Comments:**
  > **username_0**: that stopped the error but now I get no results event though I've manuallly edited entries to ensure that there entries in the category I'm testing.  
  > **username_1**: Hard to say why; I don't really understand the content model here. It might be due to the `['and'] | merge(classCats)` – in your original code, you had `['and', classCats]`. That syntax doesn't really make sense unless you're [combining multiple relational criteria](https://craftcms.com/docs/4.x/relations.html#compound-criteria), so I assumed the intent was that each "class" needs to have a relation to *all* the categories defined in `block.courseCategory`. If that's not the case (i.e. each "class" only needs to relate to at least 1 of the categories), you can just do `.relatedTo(classCats)`.  
  
  ---
  
  