laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

Search by descendants but get ancestors

Open pls-kick-me opened this issue 3 years ago • 1 comments

Hello community and thanks to creators for beautiful package! Suppose I have a tree:

  • parent1 -- child1 -- child2 --- grandson1 --- grandson2
  • parent2 and so on...

I need search children and grandsons (and next depth levels) but tree should be like above (in natural order).

After search:

Category::with('ancestors')
    ->when($request->title, function ($query) use ($request) {
        $locale = app()->getLocale();
        $title = $request->title;
        $sql = "title::json->>'$locale' ilike '%$title%'";
        $query->whereRaw($sql);
    });

I get something like:

  • grandson1 -- ancestors[ --- child1 ---- ancestors [ ----- parent1 ] ]

But I need normal tree. I must do reverse by hand or there is a way?

reversed() doesn't do the job, it is reverse in one depth but I need swap ancestors and descendants (the most depth level should be first) Thanx.

pls-kick-me avatar Jun 21 '22 05:06 pls-kick-me

Same issue here. Does anyone has any ideas?

yeftaz avatar Dec 21 '23 11:12 yeftaz