Reputation: 7128
I need help to find out my mistake in code below:
public function finderfunction(Request $request) {
// get array of categories that signed to finder (1,2,3)
$finder = Finder::with('categories')->get();
$catid = [];
foreach($finder as $findd){
foreach($findd->categories as $finddcat){
$catid[] = $finddcat->id;
}
}
//get products with those categories (1,2,3) (first where)
$results = Product::where('category_id', $catid)
//get products with those categories and same sku's (second where)
->where('sku', $request->input('brand_id'))
->groupBy('products.sku')
->get();
return view('front.finder', compact('results'));
}
As you see I commented in my code above, this is what I try to achieve:
1,2,3
1,2,3
sku
between themDuring my testing process I found out that if I replace static category id with $catid
in $results = Product::where('category_id', $catid)
I can get my results but while i'm using same code as above no result will return back, I get my custom message for those time that no result can be found.
Any idea?
Upvotes: 0
Views: 143
Reputation: 116
Firstly, Your $catid variable is an array. You can not pass an array as parameter into eloquent where. You can pass an array as
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
Secondly, Check if your $catid successfully gain value or not.
Thirdly, To write multiple where conditions, u can pass it as an array such as -
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
https://laravel.com/docs/5.6/queries
see this link for further help.
Upvotes: 1
Reputation: 1231
Try to execute your code using whereIn like this code Product::whereIn('category_id', $catid)
. Hope you will get your desired result.
Upvotes: 2