[英]Laravel Eloquent where and order conditions only if needed
Please look at following code. 请看下面的代码。 This code I have written with Codeigniter.
我用Codeigniter编写的这段代码。 What I need to highlight it where condition and order conditions works only if those post requests are set.
我需要强调的是,只有在设置了那些职位请求后,条件和订单条件才起作用。 Otherwise it i just
select * from student
. 否则,我只是
select * from student
。
Codeigniter Code Codeigniter代码
$this->db->select('*');
$this->db->from('student');
if($this->input->post('first_name') != NULL){
$first_name = $this->input->post('first_name');
$this->db->where('first_name', $first_name);
}
if($this->input->post('last_name') != NULL){
$last_name= $this->input->post('last_name');
$this->db->where('last_name', $last_name);
}
if($this->input->post('order_by') != NULL){
$order_by= $this->input->post('order_by');
$this->db->order_by($order_by);
}
$query = $this->db->get();
Laravel Code Laravel代码
I am going do the same thing with laravel. 我要和laravel做同样的事情。
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$order_by = $request->input('$order_by');
$students = Student::orderBy($order_by)
->where('first_name',$first_name)
->where('last_name',$last_name);
->paginate(10);
I able to run above code. 我能够运行以上代码。 The code works when there all post requests.
当所有发布请求都存在时,代码才起作用。
But if there is no first_name
post request I need to remove ->where('first_name',$first_name)
. 但是,如果没有
first_name
发布请求,则需要删除->where('first_name',$first_name)
。
If there i no order_by
post request, I need to remove orderBy($order_by)
. 如果没有
order_by
发布请求,则需要删除orderBy($order_by)
。
How to do it with above Laravel code. 如何使用上面的Laravel代码做到这一点。
you can use like, 你可以这样使用
$students = Student::latest();
if (isset($request->input('order_by'))) {
$students->orderBy($order_by)
}
if (isset($request->input('first_name'))) {
$students->where('first_name',$first_name);
}
if (isset($request->input('last_name'))) {
$students->where('last_name',$last_name);
}
$students->paginate(10);
Same approach: 相同的方法:
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$order_by = $request->input('$order_by');
$query = Student::orderBy($order_by);
if ($first_name) {
$query->where('first_name',$first_name);
}
if ($last_name) {
$query->where('last_name',$last_name);
}
$students = $query->paginate(10);
return $students;
You can do it like, 你可以这样
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$order_by = $request->input('order_by');
$query = DB::table('student');
if($request->first_name){
$query->where('first_name',$first_name)
}
if($request->last_name){
$query->where('last_name',$last_name);
}
if($request->order_by){
$query->orderBy($order_by);
}
$students = $query->paginate(10);
Hope you understand. 希望你能理解。
$query = Student::where('last_name',$last_name);
if ($request->has('first_name')) {
$query->where('first_name',$first_name);
}
if ($request->has('order_by')) {
$query->orderBy($order_by);
}
$students = $query->paginate(10);
An approach would be to check if the name is set and then add it to the query builder like so: 一种方法是检查名称是否已设置,然后将其添加到查询生成器中,如下所示:
$first_name = $request->input('first_name');
...
$students = Student::orderBy($order_by)
if ($first_name) {
$students->where('first_name',$first_name)
}
$students->paginate(10);
However you can clean that up! 但是,您可以清理它! Since Laravel 5.2.27 you can do as follows:
从Laravel 5.2.27开始,您可以执行以下操作:
$students = Student::orderBy($order_by)
->when($request->input('first_name'), function($query) use ($request){
return $query->where('first_name', $request->input('first_name'));
})
->paginate(10);
To make that even more readable you can use a custom macro for if-statements: 为了使它更具可读性,您可以对if语句使用自定义宏:
$students = Student::orderBy($order_by)
->if($request->input('first_name'), 'first_name', '=', $request->input('first_name'))
->paginate(10);
And the macro: 和宏:
use Illuminate\Database\Query\Builder;
Builder::macro('if', function ($condition, $column, $operator, $value) {
if ($condition) {
return $this->where($column, $operator, $value);
}
return $this;
});
Source: https://themsaid.com/laravel-query-conditions-20160425 I hope this helps! 来源: https : //themsaid.com/laravel-query-conditions-20160425我希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.