<p>Craft will include this functionality in their next release.
<a href="https://github.com/craftcms/cms/issues/7256#issuecomment-744644568" rel="nofollow noreferrer">https://github.com/craftcms/cms/issues/7256#issuecomment-744644568</a></p>

CC BY-SA 4.0
2020-12-14T19:31:41.090Z
2020-12-14T19:31:41.090Z
Bransin
<p>Element API, query entries where relationship field ':empty:' <strong>AND</strong> the same relationship field with specified entry ID.</p>
<p>Can't seem to figure out how to do this without performing two separate queries and merging the objects together. However I don't see a way to perform this within the Element API.</p>
<p>The answer to this other question seems like an approach? Option 1 or Option 2? Not quite sure as this looks all foreign to me with sparse documentation, and it's not specific to my issue.
<a href="https://craftcms.stackexchange.com/questions/30651/how-can-i-query-entries-based-on-a-matrix-block-type">How can I query entries based on a Matrix Block Type?</a></p>
<p>Seeing as you can't use ':empty:' with relatedTo, I'm stuck here.</p>
<pre><code>return [
    'criteria' =&gt; [
        'section' =&gt; 'mySection',
        /* this is the myRelatedField entries field relating to other Section Entry ID */
        'relatedTo' =&gt; (['targetElement' =&gt; $otherSectionEntryId]),
        /* unable to use :empty: with the relatedTo, incorporating myRelatedField :empty: below will return zero results */
        /* cannot use this below */
        'myRelatedField' =&gt; ':empty:'
    ],
    'transformer' =&gt; function(Entry $entry) {
        return [
            'title' =&gt; $entry-&gt;title
        ];
    }
];
</code></pre>
<p>UPDATE with further testing. Various tests used below in trying to use simple relations with :empty:. It looks as though ':empty:' is ignored when utilized in an array.</p>
<pre><code>// results returned where relationship field is empty
'relatedDrinks' =&gt; ':empty:'

// no results returned where relationship field is empty
'relatedDrinks' =&gt; [':empty:']

// results related to drink ID returned
'relatedDrinks' =&gt; [$myDrinkId]

// results returned for drink ID, no results returned with empty
'relatedDrinks' =&gt; [':empty:', $myDrinkId]
</code></pre>

<relations><plugin-elementapi>
Element API, query entries where relationship field ':empty:' AND the same relationship field with specified entry ID
# Element API, query entries where relationship field ':empty:' AND the same relationship field with specified entry ID

(Asked by: username_0 on 2020-11-16)

Element API, query entries where relationship field ':empty:' AND the same relationship field with specified entry ID.
Can't seem to figure out how to do this without performing two separate queries and merging the objects together. However I don't see a way to perform this within the Element API.
The answer to this other question seems like an approach? Option 1 or Option 2? Not quite sure as this looks all foreign to me with sparse documentation, and it's not specific to my issue.
How can I query entries based on a Matrix Block Type?
Seeing as you can't use ':empty:' with relatedTo, I'm stuck here.
return [
    'criteria' => [
        'section' => 'mySection',
        /* this is the myRelatedField entries field relating to other Section Entry ID */
        'relatedTo' => (['targetElement' => $otherSectionEntryId]),
        /* unable to use :empty: with the relatedTo, incorporating myRelatedField :empty: below will return zero results */
        /* cannot use this below */
        'myRelatedField' => ':empty:'
    ],
    'transformer' => function(Entry $entry) {
        return [
            'title' => $entry->title
        ];
    }
];

UPDATE with further testing. Various tests used below in trying to use simple relations with :empty:. It looks as though ':empty:' is ignored when utilized in an array.
// results returned where relationship field is empty
'relatedDrinks' => ':empty:'

// no results returned where relationship field is empty
'relatedDrinks' => [':empty:']

// results related to drink ID returned
'relatedDrinks' => [$myDrinkId]

// results returned for drink ID, no results returned with empty
'relatedDrinks' => [':empty:', $myDrinkId]

**Tags:** relations, plugin-elementapi

---

## 1 answers

### Answer by username_0 on 2020-12-14. Score: 1 | Accepted answer

Craft will include this functionality in their next release.
<https://github.com/craftcms/cms/issues/7256#issuecomment-744644568>


---

