我有一个带有这些参数的数据库,我想筛选它们,它一直在工作,直到我添加了“种族”和“经销商体验”,而我的一生都找不出原因。
dealer_experience是一个字符串,但我不认为这会有什么区别?
这是我的控制器代码:‘公共函数索引(请求$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'));
}
}}`
如有任何帮助,我将不胜感激,谢谢
发布于 2022-01-22 22:20:57
变化
if ($race ) {
$race = $race ->where('race',$race);
}
if ($dealer_experience) {
$dealer_experience = $dealer_experience->where('dealer_experience',$dealer_experience);
}至
if ($race ) {
$candidates = $candidates ->where('race',$race);
}
if ($dealer_experience) {
$candidates = $candidates ->where('dealer_experience',$dealer_experience);
}原因是,你仍然想过滤候选人
发布于 2022-01-23 01:28:04
您没有过滤$candidates。而且,通过使用when()方法,您可以做得更好、更易读。
$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')); https://stackoverflow.com/questions/70817570
复制相似问题