I'm using a pivot table on the project I'm working with to filter category
/*
#books
-id
-title
-description
#categories
-id
-name
#book_category
-id
-book_id
-category_id
*/
$books = $this->book->with('category')->newQuery();
if ($request->has('category')) {
$books->whereHas('category',function ($query) use ($request) {
$query->where('book_category.category_id',$request->category);
});
}
return $books->get();