lazychaser / laravel-nestedset

Effective tree structures in Laravel 4-8
3.66k stars 472 forks source link

Is getAncestors supposed to be in hierarchical order? #527

Open andgith opened 3 years ago

andgith commented 3 years ago

I have noticed the the getAncestors method on a node seems to give the ancestors in the order they were created.

Is this behaviour intended? I was wanting to use this info to generate breadcrumbs.

Version: v5.0.5

See example output from getAncestors():

[
   {
      "id":435,
      "label":"dolores veritatis",
      "type":"10",
      "_lft":901,
      "_rgt":924,
      "parent_id":null,
      "client_id":17,
      "created_at":"2021-07-21T11:49:36.000000Z",
      "updated_at":"2021-07-21T11:49:36.000000Z"
   },
   {
      "id":453,
      "label":"ad voluptatum",
      "type":"20",
      "_lft":905,
      "_rgt":912,
      "parent_id":542,
      "client_id":17,
      "created_at":"2021-07-21T11:49:36.000000Z",
      "updated_at":"2021-07-22T09:42:27.000000Z"
   },
   {
      "id":541,
      "label":"Test1",
      "type":"10",
      "_lft":902,
      "_rgt":923,
      "parent_id":435,
      "client_id":17,
      "created_at":"2021-07-22T09:23:48.000000Z",
      "updated_at":"2021-07-22T09:23:48.000000Z"
   },
   {
      "id":542,
      "label":"Test2",
      "type":"10",
      "_lft":904,
      "_rgt":921,
      "parent_id":543,
      "client_id":17,
      "created_at":"2021-07-22T09:24:05.000000Z",
      "updated_at":"2021-07-22T09:42:40.000000Z"
   },
   {
      "id":543,
      "label":"Test3",
      "type":"10",
      "_lft":903,
      "_rgt":922,
      "parent_id":541,
      "client_id":17,
      "created_at":"2021-07-22T09:40:52.000000Z",
      "updated_at":"2021-07-22T09:42:37.000000Z"
   }
]
EgorGruzdev commented 3 years ago

https://github.com/lazychaser/laravel-nestedset#default-order

Example breadcrumbs in docs https://github.com/lazychaser/laravel-nestedset#ancestors-and-descendants

andgith commented 3 years ago

Thanks for the info, I'm trying to eager load the ancestors for a collection of nodes in the correct order.

Category::with('ancestors')->paginate(30);

Does not order the eager loaded ancestors.

$result = Category::defaultOrder()->ancestorsOf($id);

Cannot be used to eager load.

I can apply defaultOrder() to the relation manually .

Category::with(['ancestors' => function ($q) {
        $q->defaultOrder();
    }])->get()
sawirricardo commented 2 years ago

Hi, thanks for this package, I think the default order was meant to be the default, without having to put that ->defaultOrder(). Are there any case when it doesn't? @lazychaser