我有一個包含這些引數的資料庫,我想過濾搜索它們,它一直在作業,直到我添加了“種族”和“經銷商經驗”,我終生無法弄清楚原因。
Dealer_experience 是一個字串,但我認為這不會有什么不同嗎?
這是我的控制器代碼:`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'));
}
}
}`
任何和所有的幫助將不勝感激,謝謝
uj5u.com熱心網友回復:
改變
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);
}
原因,您仍要篩選候選人
uj5u.com熱心網友回復:
你沒有過濾$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://www.uj5u.com/qiye/419472.html
標籤:
上一篇:如何通過按鍵有條件地呈現輸入框
下一篇:從結果中拆分引導形式
