<p>I am getting this warning message:</p>

<p>"Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.  "</p>

<p>It is pointing out all lines that have a PHP foreach function (not twig).</p>

<p>Below is one example of many other functions using the normal PHP foreach.</p>

<p>The code works and produces no front error messages.</p>

<p>This file was created in /config/myfolder/Something.php used as a custom class for "Element API"</p>

<pre><code>function myForEach($arr)
{

    $a_parent         = array();
    $a_child          = array();
    $a_accepted       = array('slug','title','groupId','id');

    // Foreach category assigned
    foreach($arr as $key =&gt; $value){

        // Foreach category values eg, uri, title etc.
        foreach($value as $k =&gt; $v){
            $a_child[$k] = $v;
        }

        // Too many items so only accept specifics stated at $a_accepted
        $f = array_intersect_key($a_child, array_flip($a_accepted));

        $a_parent[$key] = $f;

    }

    return $a_parent;
}
</code></pre>

<p>How can I adjust my code to clear this deprecation warning.</p>

CC BY-SA 4.0
2020-01-15T07:58:58.693Z
Can you provide the part of the code that calls this function?

The example that is available on github for example is like this

    ```foreach ($entry->photos->all() as $photo) {
       $photos[] = $photo->url;
    }```
    
That example would cause a deprecation warning (`photos->all()`) and exists within the transformer callback function.

I assume you use a transformer class with the function, so the calling code would be helpful
RoussKS
CC BY-SA 4.0
2020-01-19T20:08:26.200Z
Yes your are correct, I was calling a category field like this `$entry->categoryName` when it should be `$entry->categoryName->all()` . I think I got confused when the warning was pointing to a different line. Correct answer RoussKS, thank you.
Jessica Talzor
<craft3><plugin-elementapi>
Deprecation Warning: Use the all() function to fetch the query... in PHP
# Deprecation Warning: Use the all() function to fetch the query... in PHP

(Asked by: username_0 on 2020-01-14)

I am getting this warning message:
"Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.  "
It is pointing out all lines that have a PHP foreach function (not twig).
Below is one example of many other functions using the normal PHP foreach.
The code works and produces no front error messages.
This file was created in /config/myfolder/Something.php used as a custom class for "Element API"
function myForEach($arr)
{

    $a_parent         = array();
    $a_child          = array();
    $a_accepted       = array('slug','title','groupId','id');

    // Foreach category assigned
    foreach($arr as $key => $value){

        // Foreach category values eg, uri, title etc.
        foreach($value as $k => $v){
            $a_child[$k] = $v;
        }

        // Too many items so only accept specifics stated at $a_accepted
        $f = array_intersect_key($a_child, array_flip($a_accepted));

        $a_parent[$key] = $f;

    }

    return $a_parent;
}

How can I adjust my code to clear this deprecation warning.

**Tags:** craft3, plugin-elementapi
> **Comments:**
> **username_1**: Can you provide the part of the code that calls this function?  The example that is available on github for example is like this      ```foreach ($entry->photos->all() as $photo) {        $photos[] = $photo->url;     }```      That example would cause a deprecation warning (`photos->all()`) and exists within the transformer callback function.  I assume you use a transformer class with the function, so the calling code would be helpful  
> **username_0**: Yes your are correct, I was calling a category field like this `$entry->categoryName` when it should be `$entry->categoryName->all()` . I think I got confused when the warning was pointing to a different line. Correct answer RoussKS, thank you.  

---

## 0 answers

