【问题标题】:Closures in programmatically generated Eloquent OR queries以编程方式生成的 Eloquent OR 查询中的闭包
【发布时间】:2013-06-19 20:00:22
【问题描述】:

我正在尝试将查询字符串解析为涉及“或”语句的 Eloquent。 这是应用了其他过滤器等的更大功能的一部分,但我想make an or_where group

我的困惑来自于闭包......以及如何确保传递正确的值。如果我按照下面的代码进行操作,则说明闭包的参数错误,否则我无法授予它访问 $or_list 变量的权限。

$thing_query 是在此功能和其他功能过程中构建的查询。)

elseif (strstr($column, 'or|')){
        //in format  or|=tablename-id=240,tablename-id=8
            $or_list = explode(',', $value);
            var_dump(explode(',', $value));
            $thing_query->where(function($q, $or_list)
            {
                $count = 0;
                foreach ($or_list as $or) {
                    $arr = explode('=', $or);
                    $col = $arr[0];
                    $val = $arr[1];
                    $col = str_replace('-', '.', $col);
                    if($count == 0){
                       $q->where($col, '=', $val);
                    } else {
                       $q->or_where($col, '=', $val);
                    }

                    $count++;
                }
        });
}

【问题讨论】:

    标签: php closures laravel eloquent


    【解决方案1】:

    试试这个:

    $thing_query->where(function($q) use($or_list)
    {
    ...
    

    http://blog.dubbelboer.com/2012/04/07/php-use-keyword.html

    【讨论】:

    • 每一天,我都在进步
    猜你喜欢
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2020-02-12
    • 2014-09-27
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多