Laravel同时获取2个where子句的数据
我需要帮助找出我在下面代码中的错误:Laravel同时获取2个where子句的数据,laravel,Laravel,我需要帮助找出我在下面代码中的错误: 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($fi
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'));
}
正如我在上面的代码中所评论的,这就是我试图实现的目标:
1,2,3
1,2,3
sku
产品$results=Product::where('category\u id',$catid)
中用$catid
替换静态类别id,我可以得到结果,但是当我使用与上面相同的代码时,不会返回任何结果,我会得到我的自定义消息,在这些时间内找不到任何结果。
有什么想法吗?尝试使用where执行您的代码,就像下面的代码
产品::where('category\u id',$catid)
。希望您能获得所需的结果。尝试使用where执行您的代码,如以下代码产品::where('category\u id',$catid)
。希望您能得到所需的结果。首先,您的$catid变量是一个数组。不能将数组作为参数传递到Elounce where。可以将数组作为
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
其次,检查$catid是否成功获得价值
第三,要写入多个where条件,可以将其作为数组传递,例如-
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
$users=DB::table('users')->其中([
['status'、'='、'1'],
[“认购”,“1”],
])->get();
有关更多帮助,请参阅此链接。首先,$catid变量是一个数组。不能将数组作为参数传递到Elounce where。可以将数组作为
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
其次,检查$catid是否成功获得价值
第三,要写入多个where条件,可以将其作为数组传递,例如-
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
$users=DB::table('users')->其中([
['status'、'='、'1'],
[“认购”,“1”],
])->get();
请参阅此链接以获取进一步帮助。谢谢,它正在发挥作用,但已经成功了一半。现在,如果我的3个类别中有3种产品,但它们的sku相同,则只显示其中一种。为什么需要使用groupBy?取下后再打一次,希望你能得到你想要的结果。我不知道!我只需要确保我的结果将取决于sku,而不是类别id。如果你在这样的地方使用,它应该像你提供的图像一样工作。首先,它将根据WHERE子句进行过滤,然后根据sku进行过滤。它不会跳过sku匹配条件。是的,我在你评论时测试过它,似乎我不需要对它们进行分组。谢谢你的帮助。感谢manThanks,它正在发挥作用,但已经成功了一半。现在,如果我的3个类别中有3个产品,但具有相同的sku,那么它只显示其中一个。为什么需要使用groupBy?取下后再打一次,希望你能得到你想要的结果。我不知道!我只需要确保我的结果将取决于sku,而不是类别id。如果你在这样的地方使用,它应该像你提供的图像一样工作。首先,它将根据WHERE子句进行过滤,然后根据sku进行过滤。它不会跳过sku匹配条件。是的,我在你评论时测试过它,似乎我不需要对它们进行分组。谢谢你的帮助。谢谢,只是一张纸条;要获取类别id的数组,可以像这样使用pulk函数:
catId=Finder::with('categories')->get()->pulk('categories.*.id')->flatte()->toArray()代码>@ChinLeung谢谢。只需一张便条;要获取类别id的数组,可以像这样使用pulk函数:catId=Finder::with('categories')->get()->pulk('categories.*.id')->flatte()->toArray()代码>@ChinLeung谢谢。