gpt4 book ai didi

php - Laravel 集合多个 where 条件

转载 作者:可可西里 更新时间:2023-11-01 12:27:36 24 4
gpt4 key购买 nike

关注这篇文章How to create multiple where clause query using Laravel Eloquent?

我正在尝试插入多个“和”条件:

$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

最佳答案

Collection 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"}]

关于php - Laravel 集合多个 where 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44307902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com