本文介紹了Laravel Eloquent orWhere 查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
誰能告訴我如何用 Eloquent 編寫這個查詢?
Can someone show me how to write this query in Eloquent?
SELECT * FROM `projects` WHERE `id`='17' OR `id`='19'
我在想
Project::where('id','=','17')
->orWhere('id','=','19')
->get();
在這種情況下,我的變量(17 和 19)也來自多選框,所以基本上是在一個數組中.關于如何循環并動態添加這些 where/orWhere 子句的任何線索?
Also my variables (17 and 19) in this case are coming from a multi select box, so basically in an array. Any clues on how to cycle through that and add these where/orWhere clauses dynamically?
謝謝.
推薦答案
您可以通過三種方式進行.假設你有一個
You could do in three ways. Assume you've an array in the form
['myselect' =>[11, 15, 17, 19], 'otherfield' =>'測試', '_token' =>'jahduwlsbw91ihp']
可能是 Input::all();
Project::where(function ($query) {
foreach(Input::get('myselect') as $select) {
$query->orWhere('id', '=', $select);
}
})->get();
Project::whereIn('id', Input::get('myselect'))->get();
$sql = DB::table('projects');
foreach (Input::get('myselect') as $select) {
$sql->orWhere('id', '=', $select);
}
$result = $sql->get();
這篇關于Laravel Eloquent orWhere 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!