Reputation: 84
This Eloquent query does not seem to be working as expected.
If I pass in a term then category then manufacturer it works as expected, but only entering a manufacturer yields zero results and likewise with manufacturer.
Here is my url... /products?term=12&category=2&manufacturer=3
Here is the dump of toSql()
"select * from products
where id
= ? or model
like ? or category_id
= ? or manufacturer_id
= ?"
$term = $request->input('term');
$category = $request->input('category');
$manufacturer = $request->input('manufacturer');
$products = Product::orWhere('id', $term)
->orWhere('model', 'like', "%{$term}%")
->orWhere('category_id', $category)
->orWhere('manufacturer_id', $manufacturer)
->get();
Stumped, any help would be greatly appreciated.
Upvotes: 1
Views: 445
Reputation: 1156
$term = $request->input('term');
$category = $request->input('category');
$manufacturer = $request->input('manufacturer');
$products = Product::where(function ($query) use ($request,$term,$category,$manufacturer) {
if($request->filled('term'))
$query->orWhere('model', 'like', "%{$term}%")->orWhere('id', $term);
if($request->filled('category'))
$query->orWhere('category_id', $category);
if($request->filled('manufacturer'))
$query->orWhere('manufacturer_id', $manufacturer);
})->latest()
->paginate(5);
Upvotes: 1