Laravel 5.2 - 用数组过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40318642/
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 5.2 - filter with array
提问by Diego Cespedes
i'm doing a search filter, i have 3 inputs "municipality", "category", "keyword", i'm tryng to insert value to array IF input is not empty. like this:
我正在做一个搜索过滤器,我有 3 个输入“自治市”、“类别”、“关键字”,我正在尝试将值插入数组 IF 输入不为空。像这样:
public function search(Request $request)
{
$filter = array(["'visible', '=' , 1"],["'expire_date', '>', $current"]);
if(!empty($termn)){
$filter[] =["'title', 'LIKE' , '%'.$termn.'%'"];
}
if(!empty($request->input('category'))){
$filter[] = ["'category_id', '=', $input_category"];
}
if(!empty($request->input('municipality_id'))) {
$filter[] = ["'municipality_id', '=', $input_municipality"];
}
dd($filter);
$posts = Post::where($filter)->get();
}
But it is not filtering well, the dd($filter) return like this:
maybe the structure of array is not ok, i tried also like this: laravel 5.2 search querybut it doen't work. WITHOUT dd($filter) i have this error:
也许数组的结构不好,我也试过这样: laravel 5.2 搜索查询,但它不起作用。没有 dd($filter) 我有这个错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.
is null and `'municipality_id', '=', 1` is null)' at line 1 (SQL: select * from `posts` where (`'visible', '=' , 1` is null and `'expire_date', '>', 2016-10-29 13:29:30` is null and `'category_id', '=', Scegli una categoria`.
..
is null and'municipality_id', '=', 1
is null))
SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在 '.
is null and `'municipality_id', '=', 1` is null)' at line 1 (SQL: select * from `posts` where (`'visible', '=' , 1` is null and `'expire_date', '>', 2016-10-29 13:29:30` is null and `'category_id', '=', Scegli una categoria`.
..
为空且'municipality_id', '=', 1
为空))
Thank you for your help!
感谢您的帮助!
采纳答案by Yahya Uddin
You are using the where clause wrong. See the following documentation:
您使用的 where 子句错误。请参阅以下文档:
- https://laravel.com/docs/5.3/queries#where-clauses
- https://laravel.com/docs/5.3/eloquent#global-scopes
- https://laravel.com/docs/5.3/queries#where-clauses
- https://laravel.com/docs/5.3/eloquent#global-scopes
Options for where clauses in models should be sent as parameters in chained methods (NOT array values) like so:
模型中 where 子句的选项应该作为链式方法(不是数组值)中的参数发送,如下所示:
public function search(Request $request)
{
$current = Carbon::now();
$current = new Carbon();
$termn = $request->input('keyword');
$input_category = $request->input('category');
$input_municipality = $request->input('municipality_id');
$posts = Post::where('visible', 1)->where('expire_date', '>', $current);
if(!empty($termn)){
$posts->where('title', 'LIKE' , '%'.$termn.'%');
}
if(!empty($request->input('category'))){
$posts->where('category_id', '=', $input_category);
}
if(!empty($request->input('municipality_id'))) {
$posts->where('municipality_id', '=', $input_municipality);
}
$post_results = $posts->get();
dd($posts_results);
}
Note you can send a query as an array for database tables (not models) like so:
请注意,您可以将查询作为数据库表(不是模型)的数组发送,如下所示:
$users = DB::table('posts')->where([
['visible', '=', '1'],
['expire_date', '>', $current],
// ...
])->get();
回答by Amit Gupta
You can chain the where()
function in query builder
instance as:
您可以将where()
函数链接为query builder
实例:
$query = Post::where('visible', 1)->where('expire_date', '>', $current);
if(!empty($termn)){
$query->where('title', 'LIKE', '%'.$termn.'%')
}
if(!empty($request->input('category'))){
$query->where('category_id', $input_category)
}
if(!empty($request->input('municipality_id'))) {
$query->where('municipality_id', $input_municipality)
}
$posts = $query->get();