2017-06-01 65 views
1

下面这篇文章How to create multiple where clause query using Laravel Eloquent?Laravel收集多个where条件

我想插入多个 '和' 条件:

$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 
+0

那里需要两个参数,你也许可以做这样的事情回报$收藏 - >在哪里($ matchThese [0],$ matchThese [1]); – utdev

回答

0

由于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 `<>` 

所以有多个有条件的地方可以做这样的事情:

return $collection->where($matchThese[0], $matchThese[1]) 
        ->where($foo, $bar); 

你基本上可以链接它们。

+0

他正在寻找一个答案,有多个条件,而不是正确的输入格式。 – falnyr

3

收集where方法不接受像雄辩一样的条件数组。但是你可以在条件下连锁多个。

return $collection->where('destination.country', 'china') 
    ->where('doc.description', 'business'); 

$data = [ 
    ['name' => 'john', 'email' => '[email protected]'], 
    ['name' => 'john', 'email' => '[email protected]'], 
    ['name' => 'kary', 'email' => '[email protected]'], 
]; 

$collection = collect($data); 

$result = $collection->where('name', 'john'); 
// [{"name":"john","email":"[email protected]"},{"name":"john","email":"[email protected]"}] 


$result = $collection->where('name', 'john')->where('email', '[email protected]'); 
// [{"name":"john","email":"[email protected]"}] 
+0

这应该是被接受的答案。 – falnyr