I have a route setup that uses /books/category/{slug} to get books based off a slug but I'm having an issue getting the data to return quickly.
Tables
/**
* Books
*/
UUID
Name
ISBN
....
/**
* Categories
*/
UUID
Name
Slug
...
/**
* Book_Category
*/
UUID
Book_id (foreign)
Category_id (foreign)
...
I'm using the following in my controller to get the data but nothing is coming back.
public function getBooksByCategory($slug)
{
$books = Book::with('related')
->whereHas('categories', function ($query) use ($slug) {
$query->where('category.slug', $slug);
});
}
You need to add ->get() to your method and a return.
public function getBooksByCategory($slug)
{
$books = Book::with('related')
->whereHas('categories', function ($query) use ($slug) {
$query->where('category.slug', $slug)->get();
});
return $books;
}