0
votes

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 []

Try to divide your code into smaller sequences and test them separately. If every single line would work as expected it means that problem is in logic of your query. If at least one query fail it means you have to change your code.mikolaj semeniuk
ever one works alone and I think I got the problem ...... when apply only for one time so it doesn't get the result when applied many times.....but I still don't know how to make the multi-searchnew