Mixed relation criteria operator in Element API I have a bit complex filtering system that I try to replicate using Element API.

So far I have this which works to a degree:

Example url: training.json?reg=124&amp;cat=99&amp;prod=129&amp;erp=

'criteria' =&gt; [
            'section' =&gt; 'resources',
            'search' =&gt; (craft()-&gt;request-&gt;getParam('q') ? craft()-&gt;request-&gt;getParam('q')."*" : ''),
            'relatedTo' =&gt; [
                'and', 
                craft()-&gt;request-&gt;getParam('reg'),
                (craft()-&gt;request-&gt;getParam('cat') ? craft()-&gt;request-&gt;getParam('cat') : craft()-&gt;request-&gt;getParam('reg')),
                (craft()-&gt;request-&gt;getParam('prod') ? craft()-&gt;request-&gt;getParam('prod') : craft()-&gt;request-&gt;getParam('reg')),
                (craft()-&gt;request-&gt;getParam('erp') ? craft()-&gt;request-&gt;getParam('erp') : craft()-&gt;request-&gt;getParam('reg')),
            ],
  ...


First of all, I don't know how to deal with optional parameters, I've solved this by re-using the only required parameter - reg. It works, but it's not nice, any help here would be appreciated.

The real issue though is that I need the reg parameter to be an array with all the elements in the array having OR operator.

So basically:

Example url: training.json?reg[]=124&amp;reg=[]=123&amp;cat=99&amp;prod=129&amp;erp=

(reg[0] OR reg[1]) AND cat AND prod AND erp

Is there any way I can achieve this in element API? I've tried something in the lines of the following, but with no luck.

'relatedTo' =&gt; [
     'and', 
      ['targetRegions' =&gt; ['or', 124, 99]], 
      105, 
      231,
 ],
Something like this should work to combine 'and' and 'or' conditions.

'relatedTo' =&gt; [
    'and',
    ['element' =&gt; [124, 99]],
    ['element' =&gt; 105],
    ['element' =&gt; 231],
],


And this is the pattern I’m usually using to build the relatedTo param dynamically. I also try to be explicit about the direction (sourceElement / targetElement) and the relation’s field.

$query = craft()-&gt;request-&gt;getQuery();

$relatedToParam = ['and'];

if ($query['reg']) {
    $relatedToParam[] = [
        'targetElement' =&gt; $query['reg'],
        'field' =&gt; 'myRegFieldHandle',
    ];
}

// repeat for all our other relatedTo conditions

$relatedToParam = count($relatedToParam) &gt; 1 ? $relatedToParam : null,

return [
    'criteria' =&gt; [
        'section' =&gt; 'resources',
        'relatedTo' =&gt; $relatedToParam,
    ],
];