我试图与Laravel 4的高级搜索表单,这是查询:Laravel查询“if”条件?
$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;
的问题是,用户可能不会使用所有输入字段。所以我想包括一些如果条件在这部分:
$query->where('short_description', $input['search'])
->where('category', $input['category'])
->where('product', $input['product']);
..所以如果输入是空的,要删除“where”条件。
+1为此,将工作得很好,而且比我的回答更好。 – Joe
你的代码不排除空值...所以我没有得到任何结果,除非我指定所有的领域! –
对不起,但实际上你的代码是完美的,我犯了一个输入错误。非常感谢你。 –