【问题标题】:Eloquent Join - where closureEloquent Join - where 闭包
【发布时间】:2015-05-01 03:13:22
【问题描述】:

有什么方法可以在 Eloquent 的 join 子句中使用 where 的闭包?

我想做的是:

 $model = $model->leftJoin('history', function ($join) {
                $join->on('history.record_id', '=', 'work_order.work_order_id');
                $join->where('history.tablename', '=', 'test');
                $join->where('history.columnname', '=', 'column');

                $join->where(function ($q) {
                    $q->where('history.value_from', '=', '0')
                        ->orWhere('history.value_from', '=', '');
                });

                $join->where('history.value_to', '>', '0');
            });

但显然是部分:

$join->where(function ($q) {
     $q->where('history.value_from', '=', '0')
       ->orWhere('history.value_from', '=', '');
});

不起作用,因为 JoinClause 不支持 where 闭包

【问题讨论】:

  • 为什么要在那里使用闭包?你试图用它生成什么查询?会是 where 子查询吗?
  • 或者像这样将 where 语句组合在一起:... AND (history.value_from = 0 OR history.value_from = "") ...?
  • @Bogdan 我想使用类似SELECT * FROM work_order LEFT JOIN history ON history.record_id = work_order.work_order_id AND history.tablename = 'test' AND history.columnname = 'column' AND (history.value_from = 0 OR history_value_from = '') AND history_value_to > 0 ... 的东西来代替...(最后)可能是其他连接或WHERE 子句。目前,作为解决方法,我为此查询创建了关系,但是这样运行 2 个查询而不是一个
  • 一个可能的(hacky)解决方法在这里:github.com/laravel/framework/issues/4412

标签: php laravel eloquent laravel-5 query-builder


【解决方案1】:

您要查找的方法称为whereNested,可从Illumintate\Database\Query\Builder 类中获得。

不幸的是,传递给连接闭包的$join 参数是Illumintate\Database\Query\JoinClause 类型,它只有4 种方法用于处理连接whereorWherewhereNullwhereNotNull 的where 语句。

要完成这项工作,您需要使用DB::raw。这应该有效:

$model = $model->leftJoin('history', function ($join) {
                $join->on('history.record_id', '=', 'work_order.work_order_id');
                $join->where('history.tablename', '=', 'test');
                $join->where('history.columnname', '=', 'column');

                $join->where(DB::raw('(`history`.`value_from` = 0 or `history`.`value_from` = "")'), '', '');

                $join->where('history.value_to', '>', '0');
            });

【讨论】:

  • 它似乎不是这样工作的。我得到:Whoops, looks like something went wrong. 2/2 QueryException in Connection.php line 614: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and history.value_to` > ? work_order.work_order_id = 哪里?在第 1 行限制 1' . It might be a problem with binding. When I remove this where` 和 DB::raw 它工作正常。奇怪的是这个错误看起来很好之后显示的查询。
  • 尝试对第二个和第三个参数使用DB::raw(''),以避免引用不应使用的值。像这样:$join->where(DB::raw('(history.value_from = 0 or history.value_from = "")'), DB::raw(''), DB::raw(''));.
  • 不幸的是它没有改变错误。查询仍然无法运行。
  • 这在各方面都很奇怪。我已经复制了您的代码并通过调试器运行它。当调用prepare 方法时,PDO 会抛出这里的异常。但是,如果我手动设置 PDO 连接并尝试相同的步骤:new PDO(...)prepare($query)execute($bindings),使用 Eloquent 构建的完全相同的值,它就可以正常工作。
猜你喜欢
  • 2014-06-13
  • 2014-08-19
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
相关资源
最近更新 更多