使用laravel 5.6和mysql。我的表名称为vehicles
id name categoryname brandname model districtname
1 juy car toyota 121 coba
2 gty van nissan caravan natai
3 bgh car bmw 520d zenso
4 hyu van ford max mizuri
5 nhj car toyota 121 nakaga
6 gtr car toyota corolla wassa
7 gtr van nissan caravan goa
我具有以下控制器功能来过滤请求型号,
$vehicles = Vehicle::with('uploads')
->when(request('modelname'), function ($query, $request) {
return $query->where('modelname', request('modelname'));
})
->orderBy('adtype','DESC')
但是现在我需要上面的过滤器同时请求模型名称和地区名称,如何将其配置为上面的控制器?
答案 0 :(得分:0)
添加另一种when()
方法。
$vehicles = Vehicle::with('uploads')
->when(request('model'), function ($query) {
$query->where('model', request('model'));
})
->when(request('districtname'), function ($query) {
$query->where('districtname', request('districtname'));
})
->orderBy('adtype','DESC')
->get();