我使用Laravel雄辩的查询构建器,我有一个查询,我想在多个条件上有一个where子句。它能起作用,但并不优雅。
例子:
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
有没有更好的方法,或者我应该坚持这个方法?
在Laravel 5.3中(在7.x中仍然如此),你可以使用更细粒度的数组:
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
就我个人而言,我还没有发现这个用例超过多个where调用,但事实是你可以使用它。
自2014年6月起,您可以将数组传递到where
只要你想要所有的where use和operator,你可以这样分组:
$matchThese = ['field' => 'value', 'another_field' => 'another_value', ...];
// if you need another group of wheres as an alternative:
$orThose = ['yet_another_field' => 'yet_another_value', ...];
然后:
$results = User::where($matchThese)->get();
// with another group
$results = User::where($matchThese)
->orWhere($orThose)
->get();
以上将导致这样的查询:
SELECT * FROM users
WHERE (field = value AND another_field = another_value AND ...)
OR (yet_another_field = yet_another_value AND ...)
$variable = array('this' => 1,
'that' => 1
'that' => 1,
'this_too' => 1,
'that_too' => 1,
'this_as_well' => 1,
'that_as_well' => 1,
'this_one_too' => 1,
'that_one_too' => 1,
'this_one_as_well' => 1,
'that_one_as_well' => 1);
foreach ($variable as $key => $value) {
User::where($key, '=', $value);
}
whereColumn方法可以传递一个包含多个条件的数组。这些条件将使用and操作符进行连接。
例子:
$users = DB::table('users')
->whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
])->get();
$users = User::whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
])->get();
有关更多信息,请查看文档的这一部分
https://laravel.com/docs/5.4/queries#where-clauses
使用Array的条件:
$users = User::where([
'column1' => value1,
'column2' => value2,
'column3' => value3
])->get();
将产生如下查询:
SELECT * FROM TABLE WHERE column1 = value1 and column2 = value2 and column3 = value3
使用匿名函数的条件:
$users = User::where('column1', '=', value1)
->where(function($query) use ($variable1,$variable2){
$query->where('column2','=',$variable1)
->orWhere('column3','=',$variable2);
})
->where(function($query2) use ($variable1,$variable2){
$query2->where('column4','=',$variable1)
->where('column5','=',$variable2);
})->get();
将产生如下查询:
SELECT * FROM TABLE WHERE column1 = value1 and (column2 = value2 or column3 = value3) and (column4 = value4 and column5 = value5)
$projects = DB::table('projects')->where([['title','like','%'.$input.'%'],
['status','<>','Pending'],
['status','<>','Not Available']])
->orwhere([['owner', 'like', '%'.$input.'%'],
['status','<>','Pending'],
['status','<>','Not Available']])->get();
Model::where('column_1','=','value_1')
->where('column_2 ','=','value_2')
->get();
OR
// If you are looking for equal value then no need to add =
Model::where('column_1','value_1')
->where('column_2','value_2')
->get();
OR
Model::where(['column_1' => 'value_1',
'column_2' => 'value_2'])->get();
代码示例。
首先:
$matchesLcl=[];
数组在这里被填充使用所需的计数/循环条件,增量:
$matchesLcl['pos']= $request->pos;
$matchesLcl['operation']= $operation;
//+......+
$matchesLcl['somethingN']= $valueN;
更进一步,他又用了这样一种简洁的表达:
if (!empty($matchesLcl))
$setLcl= MyModel::select(['a', 'b', 'c', 'd'])
->where($matchesLcl)
->whereBetween('updated_at', array($newStartDate . ' 00:00:00', $newEndDate . ' 23:59:59'));
else
$setLcl= MyModel::select(['a', 'b', 'c', 'd'])
->whereBetween('updated_at', array($newStartDate . ' 00:00:00', $newEndDate . ' 23:59:59'));
如果你的条件是这样的(匹配一个值),一个简单的更优雅的方式是:
$results = User::where([
'this' => value,
'that' => value,
'this_too' => value,
...
])
->get();
但如果你需要OR子句,那么确保你对每个OR where()子句重复必须满足条件。
$player = Player::where([
'name' => $name,
'team_id' => $team_id
])
->orWhere([
['nickname', $nickname],
['team_id', $team_id]
])
使用Eloquent很容易创建多个where检查:
首先:(使用简单的where)
$users = User::where('name', $request['name'])
->where('surname', $request['surname'])
->where('address', $request['address'])
...
->get();
第二个:(在数组中分组where)
$users = User::where([
['name', $request['name']],
['surname', $request['surname']],
['address', $request['address']],
...
])->get();
你也可以在里面使用条件(=,<>,等等),就像这样:
$users = User::where('name', '=', $request['name'])
->where('surname', '=', $request['surname'])
->where('address', '<>', $request['address'])
...
->get();
你可以在几种情况下使用,
$results = User::where([
['column_name1', '=', $value1],
['column_name2', '<', $value2],
['column_name3', '>', $value3]
])->get();
你也可以这样用,
$results = User::orderBy('id','DESC');
$results = $results->where('column1','=', $value1);
$results = $results->where('column2','<', $value2);
$results = $results->where('column3','>', $value3);
$results = $results->get();
在Eloquent中,你可以这样做:
$results = User::where('this', '=', 1)
->orWhere('that', '=', 1)
->orWhere('this_too', '=', 1)
->orWhere('that_too', '=', 1)
->orWhere('this_as_well', '=', 1)
->orWhere('that_as_well', '=', 1)
->orWhere('this_one_too', '=', 1)
->orWhere('that_one_too', '=', 1)
->orWhere('this_one_as_well', '=', 1)
->orWhere('that_one_as_well', '=', 1)
->get();
多个地方
您还可以将一个条件数组传递给where函数。数组的每个元素都应该是一个数组,包含通常传递给where方法的三个参数:
Model::where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
多个或Where子句
如果你需要在圆括号内对"or"条件进行分组,你可以将闭包作为第一个参数传递给orWhere方法:
Model::where('votes', '>', 100)
->orWhere(function ($query) {
$query->where('name', 'abcd')
->orWhere(function ($query) {
$query->where('name', 'cdef')
->where('votes', '>', 50);
});
})
->get();
上面的例子将产生以下SQL语句:
select * from `users` where `votes` > 100 or (`name` = "abcd" or (`name` = "cdef" and `votes` > 50));
多指针where with whereNot和orWhereNot方法
whereNot和orWhereNot方法可用于对给定的查询约束组求反。例如,以下查询排除清仓或价格小于10的产品:
$products = Product::where([
['status', '=', '1'],
['is_feature', '1']
])
->whereNot(function ($query) {
$query->where('clearance', true)
->orWhere('price', '<', 10);
})
->get();