Laravel 5.2 - 带阵列的过滤器

时间:2022-10-18 17:14:22

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个输入“municipality”,“category”,“keyword”,我尝试将值插入数组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: Laravel 5.2  - 带阵列的过滤器

但它不能很好地过滤,dd($ filter)返回如下:

maybe the structure of array is not ok, i tried also like this: laravel 5.2 search query but 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服务器版本相对应的手册,以便在第1行使用'.is null和''municipality_id','=',1`为空)附近使用正确的语法(SQL:select * from`posst`其中(`'visible','=',1`为null,''expire_date','>',2016-10-29 13:29:30`为null和''category_id','=',Scegli una categoria `...为null,'municipality_id','=',1为空))

Thank you for your help!

感谢您的帮助!

2 个解决方案

#1


1  

You are using the where clause wrong. See the following documentation:

您正在使用where子句错误。请参阅以下文档:

Options for where clauses in models should be sent as parameters in chained methods (NOT array values) like so:

模型中where子句的选项应作为参数在链式方法(NOT数组值)中发送,如下所示:

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();

#2


1  

You can chain the where() function in query builder instance as:

您可以将查询构建器实例中的where()函数链接为:

$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();

#1


1  

You are using the where clause wrong. See the following documentation:

您正在使用where子句错误。请参阅以下文档:

Options for where clauses in models should be sent as parameters in chained methods (NOT array values) like so:

模型中where子句的选项应作为参数在链式方法(NOT数组值)中发送,如下所示:

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();

#2


1  

You can chain the where() function in query builder instance as:

您可以将查询构建器实例中的where()函数链接为:

$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();