Laravel在查询构建器中使用UNION [英] Laravel using UNION in query builder

查看:7257
本文介绍了Laravel在查询构建器中使用UNION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL查询工作正常,我正在尝试转换成流畅的

I have an SQL query that works fine and I'm trying to convert into 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'

这是我迄今为止流利的,这一切似乎都是正确的,我可以从文档和个人查询都可以自己的工作,只是当我UNION他们抛出一个错误:

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

当我运行它时,我得到以下错误(我也从Ardent交换回到雄辩模型在出现差异的情况下 - 它不):

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):

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 


推荐答案

看起来你的模型正在使用Ardent,而不是雄辩:

Looks like your models are using Ardent, not Eloquent:

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

可能这可能是一个问题在Ardent,而不是Laravel。

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

在这里打开一个问题: https :// github.com/laravelbook/ardent

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

编辑:

尝试更改使用QueryBuilder的雄辩:

Try to change use QueryBuilder instead of Eloquent:

将此用于QueryBuilder:

Use this for QueryBuilder:

DB::table('tags')->

而不是有说服力的方式:

Instead of the Eloquent way:

Tag::

这篇关于Laravel在查询构建器中使用UNION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆