Ad

Deactivate Filtering If Inputs Comes With All In Laravel

- 1 answer

I am trying to filter my results

$students = \App\User::leftJoin('exam_places', 'exam_places.exam_place_no', '=', 'users.exam_place_id')
                   ->select(['exam_places.*',
                             'users.*',
                            ])
                    ->where('exam_places.id',$input['examplace'])
                    ->where('users.paymenet_method',$input['payment'])
                    ->where('users.payStatus',$input['status'])->get();

my inputs comes like this for examplace (all, 1,2,3,4,5) for paymentmethod(all,1,2,3,4,..) for paystatus(0,1)

all my filtering method works however if user selects all as my

$input[examplace]= all

where statement looks for

where('exam_places.id','all')

however I want to disable this statement if all selected.

I tried to add

if($input[examplace] !=='all') {
->where('exam_places.id',$input['examplace'])
} 

it gave me error.

Edit: let me explain briefly what code I've tried

$students = \App\User::leftJoin('exam_places', 'exam_places.exam_place_no', '=', 'users.exam_place_id')
                   ->select(['exam_places.*',
                             'users.*',
                            ])
if($input['examplace'] !== 'all){
                    ->where('exam_places.id',$input['examplace'])
}
                    ->where('users.paymenet_method',$input['payment'])
                    ->where('users.payStatus',$input['status'])->get();

it gives me the error below enter image description here

so question is how can I deactivate my if statement if input comes as all ?

Ad

Answer

You are trying to add logical statements within chaining, you can`t do that without separating them Try adding the statements sequentially:

$stmt = \App\User::leftJoin('exam_places', 'exam_places.exam_place_no', '=', 'users.exam_place_id')
                 ->select(['exam_places.*','users.*',]); //break here
if($input['examplace'] !== 'all'){
    $stmt->where('exam_places.id',$input['examplace']);
}
$stmt->where('users.paymenet_method',$input['payment'])
     ->where('users.payStatus',$input['status']);
$students = $stmt->get();
Ad
source: stackoverflow.com
Ad