Laravel 集合多个where条件
Laravel collection multiple where conditions
关注这个postHow to create multiple where clause query using Laravel Eloquent?
我正在尝试插入多个 'and' 条件:
$matchThese = ['destination.country' => 'china', 'doc.description' => 'business'];
return $collection->where($matchThese);
但我收到此错误:
Too few arguments to function Illuminate\Support\Collection::where(), 1 passed . . . but two expected
由于 where
期望或需要不止一个参数,因此它不起作用。
你的错误是这样说的:
Too few arguments to function where(), 1 passed . . . but two expected
你可能会这样做:
return $collection->where($matchThese[0], $matchThese[1]);
或者这个
return $collection->where($matchThese[0], OPERATOR, $matchThese[1]); // OPERATOR could be `=` or `<>`
所以要有多个 where 条件,可以这样做:
return $collection->where($matchThese[0], $matchThese[1])
->where($foo, $bar);
你基本上可以将它们链接起来。
集合 where
方法不像 eloquent 那样接受条件数组。但是你可以链接多个 where 条件。
return $collection->where('destination.country', 'china')
->where('doc.description', 'business');
示例
$data = [
['name' => 'john', 'email' => 'john@gmail.com'],
['name' => 'john', 'email' => 'jim@gmail.com'],
['name' => 'kary', 'email' => 'kary@gmail.com'],
];
$collection = collect($data);
$result = $collection->where('name', 'john');
// [{"name":"john","email":"john@gmail.com"},{"name":"john","email":"jim@gmail.com"}]
$result = $collection->where('name', 'john')->where('email', 'john@gmail.com');
// [{"name":"john","email":"john@gmail.com"}]
链接多个 where
s 肯定会起作用,但您需要为每个链接做一个循环。请改用 filter。这将循环并仅检查一次所有条件。
$matchThese = ['destination.country' => 'china', 'doc.description' => 'business'];
return $collection->filter(function ($item) use ($matchThese) {
foreach ($matchThese as $key => $value) {
if ($item[$key] !== $value) {
return false;
}
}
return true;
});
这是我对这个问题的解决方案:
$matchThese = ['country' => 'china', 'description' => 'business'];
$data = collect([...]);
$query = null;
foreach ($matchThese as $col => $value) {
$query = ($query ?? $data)->where($col, $value);
}
在循环结束时,$query
将包含结果。
关注这个postHow to create multiple where clause query using Laravel Eloquent?
我正在尝试插入多个 'and' 条件:
$matchThese = ['destination.country' => 'china', 'doc.description' => 'business'];
return $collection->where($matchThese);
但我收到此错误:
Too few arguments to function Illuminate\Support\Collection::where(), 1 passed . . . but two expected
由于 where
期望或需要不止一个参数,因此它不起作用。
你的错误是这样说的:
Too few arguments to function where(), 1 passed . . . but two expected
你可能会这样做:
return $collection->where($matchThese[0], $matchThese[1]);
或者这个
return $collection->where($matchThese[0], OPERATOR, $matchThese[1]); // OPERATOR could be `=` or `<>`
所以要有多个 where 条件,可以这样做:
return $collection->where($matchThese[0], $matchThese[1])
->where($foo, $bar);
你基本上可以将它们链接起来。
集合 where
方法不像 eloquent 那样接受条件数组。但是你可以链接多个 where 条件。
return $collection->where('destination.country', 'china')
->where('doc.description', 'business');
示例
$data = [
['name' => 'john', 'email' => 'john@gmail.com'],
['name' => 'john', 'email' => 'jim@gmail.com'],
['name' => 'kary', 'email' => 'kary@gmail.com'],
];
$collection = collect($data);
$result = $collection->where('name', 'john');
// [{"name":"john","email":"john@gmail.com"},{"name":"john","email":"jim@gmail.com"}]
$result = $collection->where('name', 'john')->where('email', 'john@gmail.com');
// [{"name":"john","email":"john@gmail.com"}]
链接多个 where
s 肯定会起作用,但您需要为每个链接做一个循环。请改用 filter。这将循环并仅检查一次所有条件。
$matchThese = ['destination.country' => 'china', 'doc.description' => 'business'];
return $collection->filter(function ($item) use ($matchThese) {
foreach ($matchThese as $key => $value) {
if ($item[$key] !== $value) {
return false;
}
}
return true;
});
这是我对这个问题的解决方案:
$matchThese = ['country' => 'china', 'description' => 'business'];
$data = collect([...]);
$query = null;
foreach ($matchThese as $col => $value) {
$query = ($query ?? $data)->where($col, $value);
}
在循环结束时,$query
将包含结果。