Sunday, May 21, 2017

Multiple Where Clause Based On Passed Parameters in Laravel5

I want to achieve what this query does in my controller

select * from table where true
if($param1 != ""){
  and table.column1 = '$param1'
}
if($param2 != ""){
  and table.column2 = '$param2'
}

So far what i have tried is

function seachpullethouse(Request $request){
    $searchcode = $request->code;     
    $mpullethouse = DB::table('mpullethouses')->query();

    if(!empty($searchcode)){
        $query ->Where('mpullethouses.code','like','%'.$searchcode.'%');
    }

    $query ->select('mpullethouses.*')->get();
    return response()->json(['mpullethouse'=>$mpullethouse]);
}

but it seems it is wrong...Can pls anyone help me??...



via Croos Praveen

Advertisement