2016-09-06 42 views
1

我指定一個where子句:循環where子句?

$this->model = $model->where('published', '=', '1'); 
$this->model->get(); 

以上工作得很好,給人的查詢日誌:

select * from `products` where `published` = ?" 

但我需要通過的循環,從陣列中的條款。

$arr = [['published', '=', '1'], ['id', '=', '1']]; 
foreach ($arr as $key => $value) { 
    $model = $model->where($value); 
} 
$model->get(); 

但與上述我找到一個列未找到。 'where子句'中的未知列'0'。

我哪裏錯了?

而且這一點,如果我只是通過整個陣列中,它的工作原理:

$model = $model->where($arr); 

給予的查詢日誌:

"select * from `products` where (`published` = ? and `id` = ?)" 

但是,where子句是在括號中...爲什麼?

+0

我是否認爲你想將數組中的任何值傳遞給你的'where'是正確的?例如,如果你有'[['col','=','val']],那麼會導致'$ model-> where('col','=','val')'? – Jonathon

+0

是的,那是正確的,我已經可以用$ model-> where($ arr);但是這個問題是它引起我的where子句的括號。它爲什麼這樣做,括號是什麼意思? – panthro

+0

嘗試刪除賦值,使$'model = $ model-> where(...);'變成'$ model-> where(...);'。這是否做了什麼?當你執行像'$ model-> where(function($ query){// Extra where子句});'' – Jonathon

回答

0

我想你錯過了列名。
試試這個:

foreach ($arr as $key => $value) { 
    $model = $model->where($value[0],$value[1],$value[2]); 
} 
+0

這不是一個關聯數組。 – panthro

+0

@panthro抱歉沒有注意到。但是,由於'$ value'本身就是一個數組,上面的更新會執行嗎? – jaysingkar

+0

如果數組沒有3個值,該怎麼辦? – panthro

2

我好不容易纔得到的東西的工作:

\DB::enableQueryLog(); 

$model = (new User)->newQuery(); 

$wheres = [ 
    ['forename', '=', 'Joe'], 
    ['surname', '=', 'Bloggs'], 
    ['email', '=', '[email protected]'] 
]; 

foreach ($wheres as $where) { 
    // Call $model->where(...) passing in each array item as a separate param 
    call_user_func_array([$model, 'where'], $where); 
} 

$result = $model->get(); 

dd(\DB::getQueryLog(), $result); 

這將導致查詢:

select * from `users` where `forename` = ? and `surname` = ? and `email` = ? 

訣竅似乎在加入->newQuery()部分創建要在其上運行查詢的模型的實例。

注意,call_user_func_array將數組的每個項目傳遞給$model->where(...)。我發現將一個數組傳遞給->where(...)會導致構建器試圖添加幾個where子句。使用call_user_func_array的一個額外的好處還在於它會把你希望每個子句中提供然而,許多參數 - 不需要恰好3

2

解決方案1 ​​

$query = new Model; 

foreach($arr as $condition){ 
    $query->where($condition[0], $condition[1], $condition[2]); 
} 

$query->get(); 

這將創建一個查詢像下面這樣

Select * from table where published = 1 and id = 1; 

在這裏你可以看到這些值不在括號中,因爲它們不是組合結果。

解決方案2

該解決方案將建立一個聯合的where子句和最後的結果將在括號

$query = new Model; 
$query->where(function($q) use ($arr){ 
    foreach($arr as $condition){ 
     $q->where($condition[0], $condition[1], $condition[2]); 
    } 
} 

$query->get(); 

這將導致你已經實現精確查詢。這是因爲該解決方案是什麼是你的查詢中發生的故障$這個 - >模型 - >在哪裏($ ARR)

Select * from table where (published = 1 and id = 1); 

注:

爲了進一步知道爲什麼出現這種情況我們來看看下面的例如

Select * from table where (id = 2 or product_id = 3) and (publish = 1 and status = 2) 

有了這個查詢就可以看到,這是在你解決它是

的溶液1和溶液2的混合物

到目前爲止,我們已經創建了以下結果

Select * from table where (id = 1 or id = 2) 

我們添加查詢的下一部分,我們做了以下

//continued from above 
//$query has the above condition which can be continued after the first where for chaining or in next line. 
$query->where(function($q) use($input){ 
    $q->where('publish' ,'=', 1)->where('status','=',1); 
} 
這個

現在,最終的查詢就會變成一個我們需要。現在希望明確爲什麼添加括號。

+0

謝謝,只是一個簡短的問題,括號實際上在where子句的部分附近做了什麼? – panthro

+0

這就像BODMAS(Brackets然後關閉然後分裂然後乘法然後加法然後減法)。括號內容將首先被計算,然後結果將被用於最終比較。 細分 **從表中選擇*(條件)和(條件) >從表中選擇*真實和真實; ** –