我有一个问题关于laravel 4多次搜索。
我有需要检查的变量,它是空的还是空的,我想知道,还有什么其他方法可以这样做吗?
到目前为止,我试图这样做,但这似乎还不够好。
守则:
public function postQuickSearch() {
$vehicle_manufacturer = Input::get('vehicle_manufacturer');
$vehicle_model = Input::get('vehicle_model');
$year_reg_from = Input::get('year_reg_from');
$year_reg_to = Input::get('year_reg_to');
$price_from = Input::get('price_from');
$price_to = Input::get('price_to');
if(!empty($year_reg_from) && !empty($year_reg_to)) {
$query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->whereBetween('year_reg', array($year_reg_from, $year_reg_to))->get();
}
if(!empty($price_from) && !empty($price_to)) {
$query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->whereBetween('price', array($price_from, $price_to))->get();
}
if(empty($price_from) && empty($price_to) && empty($year_reg_from) && empty($year_reg_to)) {
$query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->get();
}
if($query->count()) {
$data = array(
'results' => $query
);
return View::make('auto.vehicle.search')->with($data);
}
return Redirect::route('home')->with('global', 'No results.');
}谢谢你的帮助。:)
发布于 2014-08-08 13:02:34
那么,您可以删除启动程序的代码复制。试着把所有看起来相似的东西组合起来。
示例:
public function postQuickSearch() {
$fields = array(
'vehicle_manufacturer','vehicle_model',
'year_reg_from','year_reg_to',
'price_from','price_to'
);
foreach($fields as $field)
$$field = Input::get($field);
$query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')
->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%');
if(!empty($year_reg_from) && !empty($year_reg_to)) {
$query->whereBetween('year_reg', array($year_reg_from, $year_reg_to));
}
if(!empty($price_from) && !empty($price_to)) {
$query->whereBetween('price', array($price_from, $price_to));
}
if(empty($price_from) && empty($price_to) && empty($year_reg_from) && empty($year_reg_to)) {
//Nothing yet
}
$result = $query->get();
if($result->count()) {
$data = array(
'results' => $result
);
return View::make('auto.vehicle.search')->with($data);
}
return Redirect::route('home')->with('global', 'No results.');
}https://stackoverflow.com/questions/25202022
复制相似问题