Query Builder for my shop filters Hi everyone,
I need some guidance on how to setup my queries for my shop filters.
Tables:
products
product_categories
How would i return all products with a category id from ajax request.
Lets say the id from the request is 17 how would i query the db to return all products with a product_category of 17?
thank you in advance.
@michaloravec
Quick question for you. I have the filter working but instead of outputting the $data to my loop it keeps outputting json code to the page instead. I have never seen this happen any idea how to stop it.
Here is my loop for the $data.
@foreach($data as $product)
{{$product->name}}
@endforeach
I have tried all of the following but no luck.
return response()->json(['data' => $data]);
return $data
return view('shop', compact('data'));
Any help would be appreciated as im not sure why it keeps spitting json out instead of using the loop.
I don't know what you are doing, but this should be in the controller
$products = Product::when($request->category_id, function ($query, $categoryId) {
return $query->whereHas('categories', function ($query) use ($categoryId) {
$query->where('id', $categoryId);
});
})->get();
return view('shop', compact('products'));
and this in the view
@foreach($products as $product)
{{ $product->name }}
@endforeach
Please sign in or create an account to participate in this conversation.