作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Laravel 4 中,我构建了一个搜索表单。一个人可以提交的地方
电子邮件地址从 - 至今引用编号
他们只能使用上述选项之一进行搜索;因为他们必须从他们想要搜索的下拉列表中进行选择。我用来实现此功能的最终代码如下:
if(Input::get('from') && Input::get('to')){
$records = Applications::where('created_at', '>=', Input::get('from'))
->where('created_at', '<', date('Y-m-d', strtotime(Input::get('to'). ' + 1 days')))
->lists('id');
}elseif(Input::get('email') || Input::get('ref')){
$records = Applications::where('Application_number', '=', Input::get('ref'))
->where('email', '=', Input::get('email'), 'OR')
->lists('id');
}else{
$records = Applications::all()->lists('id');
}
满足我的要求,但我不是很高兴这是最好的解决方案。
我还尝试使用此线程中的任一解决方案 Laravel 4 eloquent WHERE with OR AND OR?
$result = User::where('created_at', 'LIKE', '%' . $search . '%')
->where('updated_at', 'LIKE', '%' . $search . '%', 'OR')
->where('user_first_name', 'LIKE', '%' . $search . '%', 'AND')
->where('user_any_field', 'LIKE', '%' . $search . '%')->get();
或
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
但我一直得到的是值不能为空,当搜索 from 和 to 时;电子邮件和 ref 工作正常。
最佳答案
你可以试试这个:
$input = Input::all();
Model::where(function($query) use ($input) {
if (isset($input['something'])) {
$query->where('some_field_1', '=', $input['something']);
}
if (isset($input['something_else'])) {
$query->where('some_field_2', '=', $input['something_else']);
}
})->get();
我认为这是最干净的方式。当然,您应该根据需要放置 if 语句。
关于php - Laravel 查询数据库 'AND' 'OR' Where or Where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23578523/
我是一名优秀的程序员,十分优秀!