Framework: Laravel UNION ALL not working with where statement

Created on 1 Oct 2015  路  10Comments  路  Source: laravel/framework

I want to get two queues into one.

    $buildings_queue=IngameBuildingQueue::where(DB::raw('UNIX_TIMESTAMP(`start_time` + `duration`)'),'<=',time());
    $recruit_queue=IngameRecruitQueue::where(DB::raw('UNIX_TIMESTAMP(`start_time` + `duration`)'),'<=',time());
    $queue=$buildings_queue->unionAll($recruit_queue);
    dd($queue->toSql());

Laravel throw:

[ErrorException] Undefined property: Illuminate\Database\Eloquent\Builder::$bindings
but when I delete where() methods everything works fine.

How can I fix it?

Most helpful comment

The problem is that Database\Query\Builder::union expects a Database\Query\Builder as first param. But in this case, Database\Eloquent\Builder is passed. The Eloquent Builder don't have the $bindings attribute so it'll fail.

So my doubt is: this should be allowed? If yes, is very easy to fix it, just check if first param is a Eloquent Build and getQuery() from it. Fixed. I guess that is valid this idea, but...

Is possible to fix it basically by doing exactly what I said, directly on user code:

    $queue=$buildings_queue->unionAll($recruit_queue); // before, failing
    $queue=$buildings_queue->unionAll($recruit_queue->getQuery()); // after, fixed

All 10 comments

What version please.

Latest, 5.1

Reproducible in 02180b9b6f6d7fb6d2ccc5552ffc174cec4ce993.

Undefined property: Illuminate\Database\Eloquent\Builder::$bindings

/Illuminate/Database/Query/Builder.php:1249
/Illuminate/Database/Query/Builder.php:1262
/Illuminate/Database/Eloquent/Builder.php:943

Tested code:

        $builder = new Illuminate\Database\Eloquent\Builder(new Illuminate\Database\Query\Builder(
            m::mock('Illuminate\Database\ConnectionInterface'),
            m::mock('Illuminate\Database\Query\Grammars\Grammar'),
            m::mock('Illuminate\Database\Query\Processors\Processor')
        ));

        $builder->unionAll($builder);

Note: it's a very simplified test case when error can occur.

The problem is that Database\Query\Builder::union expects a Database\Query\Builder as first param. But in this case, Database\Eloquent\Builder is passed. The Eloquent Builder don't have the $bindings attribute so it'll fail.

So my doubt is: this should be allowed? If yes, is very easy to fix it, just check if first param is a Eloquent Build and getQuery() from it. Fixed. I guess that is valid this idea, but...

Is possible to fix it basically by doing exactly what I said, directly on user code:

    $queue=$buildings_queue->unionAll($recruit_queue); // before, failing
    $queue=$buildings_queue->unionAll($recruit_queue->getQuery()); // after, fixed

Thanks @rentalhost - it's working! Thank You.

I don't now if it should works natively. Can you reopen just for expect another opinion?

Okay I reopen it

https://github.com/laravel/framework/pull/10477 has just been merged and should fix this issue (when a new release is tagged).

$spl_invitees=DB::table('ec_meeting_special_invitees')
        ->select('meeting_id')->get();
        $meeting=DB::table('ec_meeting_master')
        ->whereNOTIn('meeting_id',function($query){
        $query->select('meeting_id')->from('ec_meeting_status_updation')
        ->where('status_id',2);

        })
        ->whereNOTIn('meeting_id',function($query) use($spl_invitees){
        $query->select('meeting_id')->from('ec_invited_members')
        ->union($spl_invitees)
        ->get();

        })

for this i am getting error

BadMethodCallException Method getBindings does not exist.

in Macroable.php (line 74)

Never mind. It was an issue on my end.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriellimo picture gabriellimo  路  3Comments

digirew picture digirew  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

ghost picture ghost  路  3Comments