2017-08-18 152 views
0

我有一個控制器中的邏輯,構建一個名爲$ exclude的數組。Laravel 5.4 where顯然不工作

爲$使用DD排除我得到:

array:4 [▼ 
    0 => 2 
    1 => 3 
    2 => 4 
    3 => 5 
] 

這是正確的。

我要排除那些結果,所以我有:

$potype = DB::table('potypes') 
       ->whereNotIn('id',[$exclude]) 
       ->get(); 

但是當我運行查詢這些項目都包含在數組中的第一個例外。所以我啓用了查詢日誌與

DB::enableQueryLog(); 

dd(DB::getQueryLog()); 

array:1 [▼ 
    0 => array:3 [▼ 
    "query" => "select * from `potypes` where `id` not in (?)" 
    "bindings" => array:4 [▼ 
     0 => 2 
     1 => 3 
     2 => 4 
     3 => 5 
    ] 
    "time" => 0.67 
    ] 
] 

表有8條記錄,但運行的查詢返回7,僅ommiting的結果列表中的第一個:

Collection {#621 ▼ 
    #items: array:7 [▼ 

如果我使用破滅

$ EX =破滅( ' '$排除) 和查詢更改爲 - > whereNotIn(' 身份證',[$ EX]) 我得到同樣的結果 - 7項只是列表中的第一個被忽略。

這是一個雄辯錯誤還是我?

回答

2

刪除[ ]並重新進行檢查:

$potype = DB::table('potypes') 
       ->whereNotIn('id',$exclude) 
       ->get(); 
+0

我 - ErrorException 無效的參數爲的foreach() –

+0

@JimElliott'''回報DD($ potype)提供;'''來檢查,如果你得到正確的查詢結果,其中是的foreach代碼? –

-2

確定它與穆罕默德的評論指着我在正確的方向realtively簡單。

作爲$exclude是一個array我改變了去:

$potype = DB::table('potypes') 
      ->whereNotIn('id',$exclude) 
      ->get(); 

,它的工作好!

+0

你應該檢查我接受的答案,不要再發送它作爲答案 –

+0

我很抱歉,但他們重疊時間 –