Laravel Eloquent where and order conditions only if needed
Laravel Eloquent where and order conditions only if needed
请看下面的代码。这段代码是我用 Codeigniter 写的。我需要强调的是,只有在设置了 post 请求的情况下,条件和订单条件才有效。否则我只是 select * from student
.
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代码
我将对 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);
我能够 运行 以上代码。该代码在所有 post 请求时有效。
但是如果没有 first_name
post 请求我需要删除 ->where('first_name',$first_name)
.
如果没有order_by
post请求,我需要删除orderBy($order_by)
.
如何使用上面的 Laravel 代码。
同样的方法:
$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;
你可以像这样使用,
$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);
你可以这样做,
$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);
希望你明白。
$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);
一种方法是检查名称是否已设置,然后将其添加到查询构建器中,如下所示:
$first_name = $request->input('first_name');
...
$students = Student::orderBy($order_by)
if ($first_name) {
$students->where('first_name',$first_name)
}
$students->paginate(10);
但是你可以清理它!由于 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);
为了使其更具可读性,您可以为 if 语句使用自定义宏:
$students = Student::orderBy($order_by)
->if($request->input('first_name'), 'first_name', '=', $request->input('first_name'))
->paginate(10);
和宏:
use Illuminate\Database\Query\Builder;
Builder::macro('if', function ($condition, $column, $operator, $value) {
if ($condition) {
return $this->where($column, $operator, $value);
}
return $this;
});
来源:https://themsaid.com/laravel-query-conditions-20160425
希望对您有所帮助!
请看下面的代码。这段代码是我用 Codeigniter 写的。我需要强调的是,只有在设置了 post 请求的情况下,条件和订单条件才有效。否则我只是 select * from student
.
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代码
我将对 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);
我能够 运行 以上代码。该代码在所有 post 请求时有效。
但是如果没有 first_name
post 请求我需要删除 ->where('first_name',$first_name)
.
如果没有order_by
post请求,我需要删除orderBy($order_by)
.
如何使用上面的 Laravel 代码。
同样的方法:
$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;
你可以像这样使用,
$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);
你可以这样做,
$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);
希望你明白。
$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);
一种方法是检查名称是否已设置,然后将其添加到查询构建器中,如下所示:
$first_name = $request->input('first_name');
...
$students = Student::orderBy($order_by)
if ($first_name) {
$students->where('first_name',$first_name)
}
$students->paginate(10);
但是你可以清理它!由于 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);
为了使其更具可读性,您可以为 if 语句使用自定义宏:
$students = Student::orderBy($order_by)
->if($request->input('first_name'), 'first_name', '=', $request->input('first_name'))
->paginate(10);
和宏:
use Illuminate\Database\Query\Builder;
Builder::macro('if', function ($condition, $column, $operator, $value) {
if ($condition) {
return $this->where($column, $operator, $value);
}
return $this;
});
来源:https://themsaid.com/laravel-query-conditions-20160425 希望对您有所帮助!