Please look at following code. This code I have written with 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
.
Codeigniter Code
$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
I am going do the same thing with 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)
.
If there i no order_by
post request, I need to remove orderBy($order_by)
.
How to do it with above Laravel code.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire