twg_
1 month ago

Query data from through pivot table based on slug

Posted 1 month ago by twg_

Hi Everyone,

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

Please sign in or create an account to participate in this conversation.