我在具有多个条件的表中执行搜索,该条件可以单独存在,也可以在组中存在。这些是我的流程代码。
控制器代码
if(\Input::has('title','category_id','type','price_max','price_min','seller_type_id','division_id','district_id','upazila_id')) {
$data['products'] = Custom::getProducts(\Input::get('title'), \Input::get('category_id'), \Input::get('type'),\Input::get('price_max'),\Input::get('price_min'),\Input::get('seller_type_id'),\Input::get('division_id'),\Input::get('district_id'),\Input::get('upazila_id'));
} else {
$data['products'] = \DB::table('products')->where('is_active', '=', 1)->get();
}
return view('pages.posts.list')->with($data);
自定义::的getProducts
public static function getProducts($title = NULL, $category_id = NULL, $type = 0, $price_max = NULL, $price_min = NULL, $seller_type_id = 0, $division_id = NULL, $district_id = NULL, $upazila_id = NULL) {
$products = DB::table('products')
->where(function($query) use ($title, $category_id, $type, $price_max, $price_min, $seller_type_id, $division_id, $district_id, $upazila_id) {
if ($title)
$query->where('title', 'like', $title);
if ($category_id)
$query->where('category_id', $category_id);
if ($type != 0)
$query->where('type', $type);
if ($price_max)
$query->where('price', '<=', $price_max);
if ($price_min)
$query->where('price', '>=', $price_min);
if ($seller_type_id != 0)
$query->where('seller_type_id', $seller_type_id);
if ($division_id)
$query->where('division_id', $division_id);
if ($district_id)
$query->where('district_id', $district_id);
if ($upazila_id)
$query->where('upazila_id', $upazila_id);
})
->where('is_active', '=', 1)->get();
return $products;
}
但它没有按预期工作。我做错了什么?