2017-06-01 64 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