here I am trying to make a multi-option search and only the first required and the rest are optional but it never works correctly and that is the last thing I reached
$search = ads::where('title','LIKE',"%".$keyword."%")
->when($category != 0,function ($query,$category){return $query->where('category_id', $category);})
->when($sub_category != 0,function ($query,$sub_category){return $query->where('sub_category_id', $sub_category);})
->when($governorate != 0,function ($query,$governorate){return $query->where('governorates_id', $governorate);})
->when($city != 0,function ($query,$city){return $query->where('cities_id', $city);})
->when($from_year != 0,function ($query,$from_year){return $query->where('year','>=', $from_year);})
->when($to_year != 0,function ($query,$to_year){return $query->where('year','<=', $to_year);})
->when($min_price > 0,function ($query,$min_price){return $query->where('price','>=', $min_price);})
->when($max_price > 0,function ($query,$max_price){return $query->where('price','<=', $max_price);})
->when($brand != 0,function ($query,$brand){return $query->where('brand', $brand);})
->when($model != 0,function ($query,$model){return $query->where('model', $model);})
->when($condition != 0,function ($query,$condition){return $query->where('condition1', $condition);})
->when($engine != 0,function ($query,$engine){return $query->where('engine', $engine);})
->when($body_type != 0,function ($query,$body_type){return $query->where('model', $body_type);})
->when($transmition != 0,function ($query,$transmition){return $query->where('transmition', $transmition);})
->when($fuel != 0,function ($query,$fuel){return $query->where('fuel', $fuel);})
->when($real_estate_type != 0,function ($query,$real_estate_type){return $query->where('real_estate_type', $real_estate_type);})
->when($space > 0,function ($query,$space){return $query->where('space', $space);})
->when($floor != 0,function ($query,$floor){return $query->where('floor', $floor);})
->when($furnished != 0,function ($query,$furnished){return $query->where('furnished', $furnished);})
->where('approval','Approved')->get();
I always get an empty array as a result []