In Book model I have 2 methods
1 for get book list another for get auth user Favourites book list. Two methods like below
For get book list :
public function getBooks($id = null)
{
$query = $this::with("bookImages","author","category")->withCount(['favourites'])->orderBy('created_at', 'desc');
return $id ? $query->findOrFail($id):$query;
}
For get user fav book list
public function getFavList()
{
return $this::join('favorites', function($query){
$query->on('books.id','=','favorites.book_id')->where('favorites.user_id', '=', 1);
})
->with("bookImages","author","category")->withCount(['favourites'])->orderBy('created_at', 'desc')
;
}
In both query with
is common. So I’m trying to reuse getBooks method in getFavList method like below
public function getFavList()
{
return $this::join('favorites', function($query){
$query->on('books.id','=','favorites.book_id')->where('favorites.user_id', '=', 1);
})
::$this->getBooks()
;
}
Here I’m getting Access to undeclared static property IlluminateDatabaseEloquentBuilder::$this
. How can I simplify this method ?
2
Answers
It’s in the official documentation under "Eager Loading"
Multiple relationships:
Nested relationships:
So for you:
Another way –
You can even try the union method:
Simple way is 😛