I am collecting all the data like id, name, date, empid from the controller in variable data
$data = $request->all();
now i want to select all the rows from the mysql table base on the input value i receive from request like this.
if no value is given then select all from DB
if ($data['keyvalue'] == '' && $data['datefrom'] =='' && $data['dateto'] =='' && $data['prepid'] =='' ){
$country = country::get();
if only certain input value is given
elseif ($data['keyvalue'] !== '' && $data['datefrom'] =='' && $data['dateto'] =='' && $data['prepid'] =='' ){
$country = country::where('sr_status','LIKE','%'.$data['status'].'%')
->Where('sr_name','LIKE','%'.$data['keyvalue'].'%')
->orWhere('tel_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('country_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('currency_name','LIKE','%'.$data['keyvalue'].'%')
->orderBy('sr_id')
->get();
}elseif ($data['keyvalue'] == '' && $data['datefrom'] !=='' && $data['dateto'] =='' && $data['prepid'] =='' ){
$country = country::where('sr_status','LIKE','%'.$data['status'].'%')
->whereDate('created_at','>=',$data['datefrom'])
->orderBy('sr_id')
->get();
}elseif ($data['keyvalue'] == '' && $data['datefrom'] =='' && $data['dateto'] !=='' && $data['prepid'] =='' ){
$country = country::where('sr_status','LIKE','%'.$data['status'].'%')
->whereDate('created_at','<=',$data['dateto'])
->orderBy('sr_id')
->get();
}elseif ($data['keyvalue'] == '' && $data['datefrom'] =='' && $data['dateto'] =='' && $data['prepid'] !=='' ){
$country = country::where('sr_status','LIKE','%'.$data['status'].'%')
->Where('prep_emp','LIKE','%'.$data['prepid'].'%')
->orderBy('sr_id')
->get();
}
and if all input value is given
elseif ($data['keyvalue'] !== '' && $data['datefrom'] !=='' && $data['dateto'] !=='' && $data['prepid'] !=='' ){
$country = country::where('sr_status','LIKE','%'.$data['status'].'%')
->whereDate('created_at','>=',$data['datefrom'])
->whereDate('created_at','<=',$data['dateto'])
->Where('sr_name','LIKE','%'.$data['keyvalue'].'%')
->orWhere('tel_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('country_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('currency_name','LIKE','%'.$data['keyvalue'].'%')
->Where('prep_emp','LIKE','%'.$data['prepid'].'%')
->orderBy('sr_id')
->get();
}
everything working fine i just want to simplify this code..
My Answer:
You can simplify the if elseif statements by using Laravel's query builder methods dynamically based on the input values. Here's an example of how you can achieve this:
php
$query = country::where('sr_status','LIKE','%'.$data['status'].'%');
if ($data['keyvalue'] !== '') {
$query->where(function($q) use ($data) {
$q->where('sr_name','LIKE','%'.$data['keyvalue'].'%')
->orWhere('tel_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('country_code','LIKE','%'.$data['keyvalue'].'%')
->orWhere('currency_name','LIKE','%'.$data['keyvalue'].'%');
});
}
if ($data['datefrom'] !== '') {
$query->whereDate('created_at','>=',$data['datefrom']);
}
if ($data['dateto'] !== '') {
$query->whereDate('created_at','<=',$data['dateto']);
}
if ($data['prepid'] !== '') {
$query->where('prep_emp','LIKE','%'.$data['prepid'].'%');
}
$country = $query->orderBy('sr_id')->get();
This way, you can dynamically build the query based on the input values without the need for multiple if elseif statements.
Rate this post
5 of 5 based on 4395 votesComments