<p><strong>UPDATE</strong> 
Figured it out based on another answer from <a href="https://craftcms.stackexchange.com/questions/15220/elementapi-handling-categorymodel-and-returning-a-heirachical-object">ElementAPI handling CategoryModel and returning a heirachical object</a></p>

<p>I wrote the query like this:</p>

<pre><code>'api/categories/&lt;entryCat:[\w-]+&gt;' =&gt; function ($entryCat) { 
  return array_merge(apiConfig(), [
      'elementType' =&gt; Category::class, 'criteria' =&gt; ['group' =&gt; $entryCat, 'level' =&gt; 1, 'with' =&gt; ['children.children.children']],
      'transformer' =&gt; function (Category $category) {
        $data = ['title' =&gt; $category-&gt;title];
        $children = $category-&gt;getChildren();
        if ($children) {
            $data['children'] = [];
            foreach ($children as $child) {
              $data['children'][] = ['title' =&gt; $child-&gt;title];
            }
        }
        return $data;
      },
  ]);
},
</code></pre>

<p>where <code>apiConfig()</code> is just common settings such as <code>pg</code> and <code>paginate</code></p>

CC BY-SA 4.0
2019-07-11T22:28:52.740Z
2019-07-11T22:28:52.740Z
kayq
<p>I am not too familiar with PHP and am just learning the Element API.
How do I turn this craft query into an element api query?</p>

<p><code>craft.categories.group("brands").level(1).all(),</code></p>

<p>In categories this is what it looks like</p>

<pre><code>Brands
    -Luxury
        -Gucci
        -Chanel
        -Louis Vuitton
    -Streetwear
        -Supreme
        -Bape
        -Off White
OtherCategory
    - ...
SomeOtherCategory
    - ... 
    - ...
</code></pre>

<craft3><plugin-elementapi><elementquery>
How do I fetch all categories under a category group using the Element API?
# How do I fetch all categories under a category group using the Element API?

(Asked by: username_0 on 2019-07-09)

I am not too familiar with PHP and am just learning the Element API.
How do I turn this craft query into an element api query?
craft.categories.group("brands").level(1).all(),
In categories this is what it looks like
Brands
    -Luxury
        -Gucci
        -Chanel
        -Louis Vuitton
    -Streetwear
        -Supreme
        -Bape
        -Off White
OtherCategory
    - ...
SomeOtherCategory
    - ... 
    - ...

**Tags:** craft3, plugin-elementapi, elementquery

---

## 1 answers

### Answer by username_0 on 2019-07-11. Score: 0 | Accepted answer

UPDATE 
Figured it out based on another answer from ElementAPI handling CategoryModel and returning a heirachical object
I wrote the query like this:
'api/categories/' => function ($entryCat) { 
  return array_merge(apiConfig(), [
      'elementType' => Category::class, 'criteria' => ['group' => $entryCat, 'level' => 1, 'with' => ['children.children.children']],
      'transformer' => function (Category $category) {
        $data = ['title' => $category->title];
        $children = $category->getChildren();
        if ($children) {
            $data['children'] = [];
            foreach ($children as $child) {
              $data['children'][] = ['title' => $child->title];
            }
        }
        return $data;
      },
  ]);
},

where apiConfig() is just common settings such as pg and paginate


---

