I have a database with these parameters and I'd like to filter search them, it has been working until I added "race" and "dealer experience" and I can't for the life of me figure out why.
dealer_experience is a string, but I don't think that would make a difference ?
Here is my controller code: ` public function index(Request $request){
$title = $request->get('title');
$type = $request->get('type');
$category = $request->get('category_id');
$province = $request->get('province');
$brand= $request->get('brand_id');
$address = $request->get('address');
$race = $request->get('race');
$dealer_experience = $request->get('dealer_experience');
if($title||$type||$category||$address||$brand||$race||$dealer_experience) {
$candidates = Profile::query();
if ($title) {
$candidates = $candidates->where('title','LIKE','%'.$title.'%');
}
if ($category) {
$candidates = $candidates->where('category_id',$category);
}
if ($brand) {
$candidates = $candidates->where('brand_id',$brand);
}
if ($type) {
$candidates = $candidates->where('type',$type);
}
if ($address) {
$candidates = $candidates->where('address','LIKE','%'.$address.'%');
}
if ($race ) {
$race = $race ->where('race',$race);
}
if ($dealer_experience) {
$dealer_experience = $dealer_experience->where('dealer_experience',$dealer_experience);
}
$candidates = $candidates->where('profile_status',1)->paginate(5);
return view('profile.allcandidates',compact('candidates'));
}
else
{
$candidates= Profile::latest()->where('profile_status',1)->paginate(2);
return view('profile.allcandidates',compact('candidates'));
}
}
}`
Any and all help would be appreciated, thank you
CodePudding user response:
Change
if ($race ) {
$race = $race ->where('race',$race);
}
if ($dealer_experience) {
$dealer_experience = $dealer_experience->where('dealer_experience',$dealer_experience);
}
to
if ($race ) {
$candidates = $candidates ->where('race',$race);
}
if ($dealer_experience) {
$candidates = $candidates ->where('dealer_experience',$dealer_experience);
}
Reason, you still want to filter the candidates
CodePudding user response:
You are not filtering $candidates. Also you could do it nicer and more readable by using the when() method.
$candidates = Profile::where('profile_status',1)
->when($request->has('title'), function($query) use ($request){
$query->where('title', 'LIKE', '%'.$request->title.'%');
})
->when($request->has('type'), function($query) use ($request){
$query->where('type', $request->type);
})
->when($request->has('category_id'), function($query) use ($request){
$query->where('category_id', $request-category_id);
})
->when($request->has('brand_id'), function($query) use ($request){
$query->where('brand_id', $request->brand_id);
})
->when($request->has('address'), function($query) use ($request){
$query->where('address', 'LIKE', '%'.$request->address.'%');
})
->when($request->has('race'), function($query) use ($request){
$query->where('race', $request->race);
})
->when($request->has('dealer_experience'), function($query) use ($request){
$query->where('dealer_experience', $request->dealer_experience);
});
if ($request->hasAny(['title', 'type', 'category', 'address', 'brand', 'race', 'dealer_experience'])) {
$candidates = $candidates->paginate(5);
} else {
$candidates = $candidates->latest()->paginate(2);
}
return view('profile.allcandidates',compact('candidates'));
