John Smith
John Smith

Reputation: 808

Laravel 4.2 union

For the following request:

$gamer_id = DB::table('users_relations')->select('gamer_id')->where('user_id', '=', Auth::user()->id)->first();
$test_id = DB::table('users_relations')->select('gamer_id')->where('user_id', '=', 2)->first();
$results = $gamer_id->union($test_id)->get(); 

dd($results);

I have the error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method stdClass::union()

Any thought about that ? Thank you!

P.S. Documentation -−> http://laravel.com/docs/queries#unions

Upvotes: 2

Views: 832

Answers (1)

ceejayoz
ceejayoz

Reputation: 179994

When you call first(), you get() and return the first result. $gamer_id is no longer a query builder as a result, so it lacks the union function. Instead, use the query modifier take(1).

$gamer_id = DB::table('users_relations')
  ->select('gamer_id')
  ->where('user_id', '=', Auth::user()->id)
  ->take(1);
$test_id = DB::table('users_relations')
  ->select('gamer_id')
  ->where('user_id', '=', 2)
  ->take(1);
$results = $gamer_id->union($test_id)->get(); 

Upvotes: 3

Related Questions