0

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
    - ... 
    - ...
0

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/<entryCat:[\w-]+>' => 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.