我正在尝试使用 Laravel 4 制作高级搜索表单,这是查询:
$result = DB::table('users_ads')
->join('ads', 'users_ads.ad_id', '=', 'ads.id')
->orderBy($column, $method)
->where('status', TRUE)
->where(function($query) use ($input)
{
$query->where('short_description', $input['search'])
->where('category', $input['category'])
->where('product', $input['product']);
})
->join('users', 'users_ads.user_id', '=', 'users.id')
->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')
->get();
return $result;
问题是用户可能不会使用所有输入字段。所以我想在这部分包含一些 if 条件:
$query->where('short_description', $input['search'])
->where('category', $input['category'])
->where('product', $input['product']);
..因此,如果输入为空,则删除“where”条件。