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

How to get includes relation sub resources of a root node?

Open lichnow opened this issue 5 years ago • 1 comments

the Category model is nested,and Book model relation it by "many to many". now I want to get all books of root category node exp: id = 1 and its descendant categories. I only can do it by a model function

public function getContainBooks()
    {
        return Book::whereHas('categories', function ($q) {
            $q->whereIn('id', $this->whereDescendantOrSelf($this->id)->get()->pluck('id'));
        });
    }

Is there any solution that can be done using scope? Like call Category::withBooks()->find(1) and a scope method scopeWithBooks($query) in category model?

lichnow avatar Apr 03 '19 17:04 lichnow

any update on this issue?

athasamid avatar Mar 15 '21 05:03 athasamid