atayahmet / laravel-nestable

Laravel 5 nested category/menu generator
MIT License
214 stars 52 forks source link

showing subcategories of a number of categories #19

Closed ahmadyousefdev closed 7 years ago

ahmadyousefdev commented 7 years ago

Hi, for example: I want to show a list of all the sub-categories (children) under the categories number [1,2,3], when I use

$id = [1,2,3];
        $categories = Category::whereIn('id',$id)->renderAsJson();
        return $categories;

it returns only those three categories [1,2,3] and it doesn't return any children.

the same result here :

$categories = Category::whereIn('id',$id)->nested()->get();

There is a function in this package: parent() , but it only works with one parent_id like this:

$id = 1;
$categories = Category::parent($id)->renderAsJson();

Is there any way to fix this?

Thanks for this amazing and easy to use package !

atayahmet commented 7 years ago

Hi @ahmadalukah

This package is not support hierarchy queries. Only sorts the current data as to hierarchy.

For example:

I suppose we have some data like:

id parent_id name
1 0 X
2 1 Y
3 2 Z

and run the your query:

$id = [1,2,3];
$categories = Category::whereIn('id',$id)->renderAsArray();
array:1 [
      0 => array:5 [
        "id" => 1
        "name" => "X"
        "child" => array:1 [
          0 => array:1 [
            "id" => 2
            "name" => "Y"
            "child" => [
                 0 => array:1 [
                 "id" => 3
                 "name" => "Z"
                 "parent_id" => 2,
                 "child" => []
            ]
            "parent_id" => 1
          ]
        ]
        "parent_id" => 0
      ]
]

I hope, i can tell you.