我目前正在使用大量if
/ elseif
语句过滤数据库中的用户。希望有一种更简单的方法。现在,我正在为每个可能的查询编写具有以下5个值的if语句:
请注意,我正在使用Laravel 5.7
HTML:按预先填充的选择下拉列表进行过滤
<!-- LOCATION -->
<div>
<select id="location" name="location">
<option value="">No Preference</option>
@foreach ($country as $c)
<option value="{{ $c->country_name }}">{{ $c->country_name }}</option>
@endforeach
</select>
</div>
<!-- GENDER -->
<div>
<select id="gender" name="gender">
<option value="">No Preference</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<!-- AGE -->
<div>
<select id="age" name="age">
<option value="">No Preference</option>
<option value="1">1</option>
<option value="2">2</option>
ETC...
</select>
</div>
<!-- BIRTH MONTH -->
<div>
<select id="month" name="month">
<option value="">No Preference</option>
<option value="01">January</option>
<option value="02">February</option>
ETC...
</select>
</div>
<!-- BIRTH DATE -->
<select id="day" name="day">
<option value="">No Preference</option>
<option value="1">1</option>
<option value="2">2</option>
ETC...
</select>
</div>
<!-- FILTER RESULTS BTN -->
<div>
<button id="ajaxSubmit">
Filter Results
</button>
</div>
JS过滤结果
$('#ajaxSubmit').click(function() {
console.log('clicked');
var location = $('#location').val();
var gender = $('#gender').val();
var age = $('#age').val()
var month = $('#month').val()
var day = $('#day').val()
$.get('/child-sponsorship/filter?location=' + location + '&gender=' + gender + '&age=' + age + '&month=' + month + '&day=' + day, function(data){
console.log(data);
$('#kids_results').empty();
$('#kids_results').html(data);
});
});
控制器:此方法有效,但这是if语句的噩梦
细致的滤网可能会发生各种变化...
public function filter(Request $request){
$location = $request->location;
$gender = $request->gender;
$age = $request->age;
$month = $request->month;
$day = $request->day;
$country = DB::table('countries')->orderBy('country_name')->get();
//IF ALL FILTERS ARE EMPTY
if(empty($location) && empty($gender) && empty($age) && empty($month) && empty($day)){
$sponsorKid = Kid::orderBy('id')->get();
}//IF ALL FILTER ARE TRUE
elseif(!empty($location) && !empty($gender) && !empty($age) && !empty($month) && !empty($day)){
$sponsorKid = DB::table('kids')
->where('current_country', $location)
->where('gender', $gender)
->where('age', $age)
->where('month', $month)
->where('day', $day)
->orderby('id')
->get();
}//IF LOCATION ONLY IS TRUE
elseif(empty($gender) && empty($age) && empty($month) && empty($day)){
$sponsorKid = DB::table('kids')
->where('current_country', $location)
->orderby('id')->get();
}//IF GENDER ONLY IS TRUE
elseif(empty($location) && empty($age) && empty($month) && empty($day))
//etc.....
//return Response::json($sponsorKid);
$view = View::make('child-sponsorship.filter_results', compact('sponsorKid'));
$view = $view->render();
return $view;
}
有更简便的方法吗?
我尝试使用LIKE
过滤控制器,但没有运气:
$sponsorKid = Kid::query()
->where('countries', 'LIKE', "%{$location}%")
->orWhere('gender', 'LIKE', "%{$gender}%")
->orWhere('age', 'LIKE', "%{$age}%")
->orWhere('month', 'LIKE', "%{$month}%")
->orWhere('day', 'LIKE', "%{$day}%")
->orderby('id')
->get();
$view = View::make('child-sponsorship.filter_results', compact('sponsorKid'));
$view = $view->render();
return $view;
任何帮助将不胜感激。
答案 0 :(得分:4)
何时查询构建器方法
$sponsorKid = Kid::when($location,function($q,$location) {
return $q->where('countries', 'LIKE', "%."$location."%")
})->when($gender,function($q,$gender) {
return $q->orWhere('gender', 'LIKE', "%".$gender."%")
})->when($age,function($q,$age) {
return $q->orWhere('age', 'LIKE', "%".$age."%")
})->when($month,function($q,$month) {
return $q->orWhere('month', 'LIKE', "%".$month."%")
})->when($day,function($q,$day) {
return $q->orWhere('day', 'LIKE', "%".$day."%")
})->orderby('id')
->get();
答案 1 :(得分:1)
我相信您有一个名为Kid.php的儿童表模型,您可以使用该模型启动查询并执行以下操作:
//create a query of Kid model
$sponsorKid = Kid::query();
// add a where clause for location only if location is not empty,
//otherwise you have the default query
if(!empty($location)) {
$sponsorKid = $sponsorKid->where('location', $location);
}
// add a where clause for gender only if gender is not empty,
//otherwise you have the default query, or if this condition and
//above is true then you will have a query which has 2 wheres
if(!empty($gender)) {
$sponsorKid = $sponsorKid->where('gender', $gender);
}
if(!empty($age)) {
$sponsorKid = $sponsorKid->where('age', $age);
}
if(!empty($month)) {
$sponsorKid = $sponsorKid->where('age', $age);
}
if(!empty($day)) {
$sponsorKid = $sponsorKid->where('day', $day);
}
//now that you have a final query pull the records
$sponsorKid = $sponsorKid->orderBy('id')->get();
$view = View::make('child-sponsorship.filter_results', compact('sponsorKid'));
$view = $view->render();
return $view;