1

我有一个运行良好的 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'

到目前为止,这就是我流利的内容,据我从文档中可以看出,这一切似乎都是正确的,并且各个查询都可以自行工作,只是当我将它们联合起来时,它会引发错误:

   $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 切换回 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 
4

2 回答 2

4

Looks like your models are using Ardent, not Eloquent:

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

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

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

EDIT:

Try to change use QueryBuilder instead of Eloquent:

Use this for QueryBuilder:

DB::table('tags')->

Instead of the Eloquent way:

Tag::
于 2013-09-27T12:25:30.877 回答
1

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

$pdo = DB::connection()->getPdo();
于 2013-09-27T14:39:45.273 回答