2017-08-30 67 views
1

請看下面的代碼。我用Codeigniter編寫的這段代碼。我需要在條件和訂單條件有效的情況下突出顯示,只有在設置了這些發佈請求的情況下。否則,我只是select * from studentLaravel雄辯只有在需要的地方和訂單條件

笨代碼

$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); 

我能夠運行上面的代碼。代碼適用於所有發佈請求。

但是,如果沒有first_name發佈請求,我需要刪除->where('first_name',$first_name)

如果有我沒有order_by發佈請求,我需要刪除orderBy($order_by)

如何用上面的Laravel代碼實現它。

回答

5

你可以使用像,

$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); 
0

同樣的方法:

$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; 
0

你可以不喜歡它,

$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); 

希望你能理解。

0
$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); 
0

的方法是檢查名稱設置,然後將其添加到查詢生成器,像這樣:

$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 我希望這有助於!