Laravel using UNION in query builder
Solution 1
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::
Solution 2
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 = DB::connection()->getPdo();
Comments
-
Al_ almost 2 years
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'
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):
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
-
Al_ over 10 yearsHi, I've tried setting all three models to extend Eloquent not Ardent I I get a similar error:
Argument 1 passed to Illuminate\Database\Query\Builder::mergeBindings() must be an instance of Illuminate\Database\Query\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in E:\docs\Documents\Work\MGC\mgc_root\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 898 and defined
-
Antonio Carlos Ribeiro over 10 yearsAre you using Laravel 4.0 or 4.1?
-
Al_ over 10 years4.0 I believe, I'll try running a composer update to see if that helps
-
Antonio Carlos Ribeiro over 10 yearsEdited, might work using QueryBuilder. But this looks like a bug on Laravel.
-
Al_ over 10 yearsI'm on 4.0 the composer.json file has :
"laravel/framework": "4.0.*",
, I've run a composer update. Guess I'll file a bug report, thanks. -
Antonio Carlos Ribeiro over 10 yearsDid you saw my edit? Tested using QueryBuilder instead of Eloquent?
-
Al_ over 10 yearsHi, just noticed it. That does seem to work - strange! Is that a bug that should be reported do you think or should I close the bug request I opened on github. Thanks!!
-
Antonio Carlos Ribeiro over 10 yearsKeep it. There's a problem on Eloquen/QueryBuilder.
-
Antonio Carlos Ribeiro over 10 yearsNo problem. Enjoy Laravel! :)
-
lu cip over 10 yearsI got the same issue, anyway this is because
mergeBindings()
function expect an instance ofIlluminate\Database\Query\Builder
instead ofArdent\Builder
orEloquent\Builder
... as you said before. SoDB::table('tags')
can replaceTag
just for variable$itemListTags
sent tounion()
function. I said that because sometimes (as in my case) you need Eloquent or Ardent for the first object ( as$itemTags
in this case ), and this can stayEloquent
orArdent
. Thanks for your answer.