标题说明了一切。
我知道我能做到:
DB::table('items')->where('something', 'value')->get()
但是我想检查多个值的 where 条件,如下所示:
DB::table('items')->where('something', 'array_of_value')->get()
有什么简单的方法吗?
There's whereIn():
whereIn()
$items = DB::table('items')->whereIn('id', [1, 2, 3])->get();
You can use whereIn which accepts an array as second paramter.
whereIn
DB:table('table') ->whereIn('column', [value, value, value]) ->get()
You can chain where multiple times.
DB:table('table')->where('column', 'operator', 'value') ->where('column', 'operator', 'value') ->where('column', 'operator', 'value') ->get();
This will use AND operator. if you need OR you can use orWhere method.
AND
OR
orWhere
For advanced where statements
where
DB::table('table') ->where('column', 'operator', 'value') ->orWhere(function($query) { $query->where('column', 'operator', 'value') ->where('column', 'operator', 'value'); }) ->get();
If you need by several params:
$ids = [1,2,3,4]; $not_ids = [5,6,7,8]; DB::table('table')->whereIn('id', $ids) ->whereNotIn('id', $not_ids) ->where('status', 1) ->get();
You could use one of the below solutions:
$items = Item::whereIn('id', [1,2,..])->get();
or:
$items = DB::table('items')->whereIn('id',[1,2,..])->get();
WHERE AND SELECT Condition In Array Format Laravel
use DB; $conditions = array( array('email', '=', 'user@gmail.com') ); $selected = array('id','name','email','mobile','created'); $result = DB::table('users')->select($selected)->where($conditions)->get();
If you are searching by ID you can also use:
$items = Item::find(array_of_ids);
$whereData = [['name', 'test'], ['id', '<>', '5']];
$users = DB::table('users')->where($whereData)->get();
It's work for me fetch just id then search in array
$shops = Shop::Where('user_id', Auth::id())->get('id'); $categories = Category::whereIn('shop_id',$shops)->paginate(7);