【问题标题】:Laravel 5.2 - filter with arrayLaravel 5.2 - 使用数组过滤
【发布时间】:2017-03-12 03:31:22
【问题描述】:

我正在做一个搜索过滤器,我有 3 个输入“市政”、“类别”、“关键字”,我正在尝试将值插入到数组中,如果输入不为空。像这样:

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

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

也许数组的结构不对,我也试过这样: laravel 5.2 search query 但它不起作用。 没有 dd($filter) 我有这个错误:

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`... 附近使用的正确语法为 null,'municipality_id', '=', 1 为 null))

感谢您的帮助!

【问题讨论】:

  • 考虑更好地编写您的问题。那里有这么多。像“输入”和检查东西是否为空。你刚才说$filter = [..]; $posts = Post::where($filter)->get();给了某某错误不是更好吗。我究竟做错了什么?这样你会得到更多的答案。

标签: php arrays laravel


【解决方案1】:

您可以将query builder 实例中的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();

【讨论】:

    【解决方案2】:

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

    模型中 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);
    }
    

    请注意,您可以将查询作为数据库表(不是模型)的数组发送,如下所示:

    $users = DB::table('posts')->where([
        ['visible', '=', '1'],
        ['expire_date', '>', $current],
        // ...
    ])->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 2017-02-14
      • 2020-04-15
      • 2021-12-09
      • 2021-08-18
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多