Home > Mobile >  how to handle null value in Laravel Eloquent?
how to handle null value in Laravel Eloquent?

Time:09-22

i am new in laravel.Actually i want to filter some data on state and typeofsupply column. like if state="south australia" and typeofsupply="Inverter & Storage" then show me these rows who exist on both value.and if i filter like state="south australia" and typeofsupply=Nul.then these rows will be display who exist state of south australia and typeofsupply should be any of list(All type of supply)

public function show(Request $request){

    $typeofsupply='';
    $state='';
    if(!$request->all()==Null) {

        if(!$request->state==Null) {
            $state=$request->state;
        }
        if(!$request->typeofsupply==Null) {
            $typeofsupply=$request->typeofsupply;
        }

        $items = Postjob::where('state',$state)
                        ->where('typeofsupply',$typeofsupply)
                        ->get();
        dd($items);

CodePudding user response:

this is how i resolved my problem....

$items = Postjob::when($state!=Null,function ($query) use ($state) {
                    return $query->where('state', $state);})->
                when($typeofsupply!=Null,function ($query) use ($typeofsupply) {
                    return $query->where('typeofsupply', $typeofsupply);})->get();
  • Related