search in pivot table

Submitted by dickyeka - 5 months ago

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();

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks