laravel-nestedset
laravel-nestedset copied to clipboard
How to get includes relation sub resources of a root node?
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?
any update on this issue?