问题
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
回答1:
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();
来源:https://stackoverflow.com/questions/24166337/laravel-4-2-union