Laravel 在查询构建器中使用 UNION

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19050387/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:29:19  来源:igfitidea点击:

Laravel using UNION in query builder

phpsqllaravellaravel-4eloquent

提问by Al_

I have an SQL query that works fine and I'm trying to convert into fluent::

我有一个运行良好的 SQL 查询,我正在尝试转换为 fluent::

SELECT DISTINCT tags.tag
  FROM tags, items
  WHERE tags.taggable_type = 'Item'
  AND items.item_list_id = '1'
UNION
SELECT DISTINCT tags.tag
  FROM tags, itemlists
  WHERE tags.taggable_type = 'ItemList'
  AND itemlists.id = '1'

This is what I have so far in fluent, it all seems right as far as I can tell from the docs and the individual queries both work on their own, it's just when I UNION them it throws an error:

这就是我到目前为止所掌握的流利,据我从文档和个人查询中可以看出,这一切似乎都是正确的,只是当我联合它们时,它会引发错误:

   $itemTags = Tag::join('items', 'items.id', '=', 'tags.taggable_id')
                ->select('tags.tag')
                ->distinct()
                ->where('tags.taggable_type', '=', 'Item')
                ->where('items.item_list_id', '=', $itemList->id);

    $itemListTags = Tag::join('itemlists', 'itemlists.id', '=', 'tags.taggable_id')
                ->select('tags.tag')
                ->distinct()
                ->where('tags.taggable_type', '=', 'ItemList')
                ->where('itemlists.id', '=', $itemList->id);
// the var_dump below shows the expected results for the individual queries
// var_dump($itemTags->lists('tag'), $itemListTags->lists('tag')); exit;
    return      $itemTags
                ->union($itemListTags)
                ->get();

I get the following error when I run it (I've also swapped from Ardent back to Eloquent on the model in case that made a difference - it doesn't):

我在运行它时收到以下错误(我还在模型上从 Ardent 切换回 Eloquent 以防有所不同 - 它没有):

Argument 1 passed to Illuminate\Database\Query\Builder::mergeBindings() must be an instance of Illuminate\Database\Query\Builder, instance of LaravelBook\Ardent\Builder given, called in path/to/root\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 898 and defined 

回答by Antonio Carlos Ribeiro

Looks like your models are using Ardent, not Eloquent:

看起来您的模型使用的是 Ardent,而不是 Eloquent:

...instance of LaravelBook\Ardent\Builder given, ...

And probably this might be a problem on Ardent, not Laravel.

这可能是 Ardent 的问题,而不是 Laravel。

Open an issue here: https://github.com/laravelbook/ardent.

在此处打开一个问题:https: //github.com/laravelbook/ardent

EDIT:

编辑:

Try to change use QueryBuilder instead of Eloquent:

尝试更改使用 QueryBuilder 而不是 Eloquent:

Use this for QueryBuilder:

将此用于 QueryBuilder:

DB::table('tags')->

Instead of the Eloquent way:

而不是雄辩的方式:

Tag::

回答by Uze

I know you mentioned wanting to use the query builder, but for complex queries that the builder might throw fits on, you can directly access the PDO object:

我知道您提到要使用查询构建器,但是对于构建器可能会抛出的复杂查询,您可以直接访问 PDO 对象:

$pdo = DB::connection()->getPdo();