Laravel:高级搜索表单查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26972963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel : advanced search form query
提问by maximus 69
I have an advanced search form to filter out results from a database using Laravel. The data is filtered correctly but I have a requirement for the user to be able to filter by first name or last name using the same text box (in the advanced form). I tried orWhere to make sure it filters the name field with the first name or last name but the orWhere doesn't consider the other filters. The code I am using is as follows:
我有一个高级搜索表单,可以使用 Laravel 从数据库中过滤出结果。数据已正确过滤,但我要求用户能够使用相同的文本框(以高级形式)按名字或姓氏进行过滤。我试过 orWhere 以确保它使用名字或姓氏过滤名称字段,但 orWhere 不考虑其他过滤器。我使用的代码如下:
DB::table('mytable')
->where(function($query) use ($name, $degree_s, $specialty_s, $city_s, $state_s, $lundbeck_id_s) {
if ($name)
$query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%"); # this is whats causing the issue
if ($specialty_s)
$query->where('primary_specialty', $specialty_s);
if ($city_s)
$query->where('city', $city_s);
if ($state_s)
$query->where('state_province', $state_s);
if ($lundbeck_id_s)
$query->where('customer_master_id', $lundbeck_id_s);
if ($degree_s)
$query->where('primary_degree', $degree_s);
})
->select('id', 'first_name','last_name')
Adding the orWhere clause causes the query to not use the other conditions as well (like city_s or state_s).
添加 orWhere 子句会导致查询不使用其他条件(如 city_s 或 state_s)。
回答by Marcin Nabia?ek
You need to change:
你需要改变:
if ($name)
$query->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");
into:
进入:
if ($name) {
$query->where(function($q) use ($name) {
$q->where('first_name', 'like', "$name%")->orWhere('last_name', 'like', "$name%");
});
}
to make Laravel to add parentheses so it will work as you expect.
让 Laravel 添加括号,使其按预期工作。
EDIT
编辑
Of course you don't need to wrap everything with closure here, so the best solution for that would be:
当然,您不需要在这里用闭包包装所有东西,因此最好的解决方案是:
<?php
$query = DB::table('mytable')->select('id', 'first_name', 'last_name');
if ($name) {
$query->where(function ($q) use ($name) {
$q->where('first_name', 'like', "$name%")
->orWhere('last_name', 'like', "$name%");
});
}
if ($specialty_s) {
$query->where('primary_specialty', $specialty_s);
}
if ($city_s) {
$query->where('city', $city_s);
}
if ($state_s) {
$query->where('state_province', $state_s);
}
if ($lundbeck_id_s) {
$query->where('customer_master_id', $lundbeck_id_s);
}
if ($degree_s) {
$query->where('primary_degree', $degree_s);
}
$data = $query->get();
回答by itachi
i will build the query little bit differently.
我会以稍微不同的方式构建查询。
$q = DB::table('mytable')->select(['id', 'first_name', 'last_name']);
if(!empty($name))
{
$q->where(function($query)use($name){
$query->where(('first_name', 'like', "$name%")
->orWhere('last_name', 'like', "$name%");
});
}
if(!empty($specialty_s) $q->where('primary_specialty', $specialty_s);
so far.... so forth.
到目前为止......等等。
at last
最后
return q->get()
or $q->paginate(30)
depending upon your needs.
返回q->get()
或$q->paginate(30)
根据您的需要。