2014-06-11 78 views
2

對於下列要求:Laravel 4.2工會

$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); 

我有錯誤:

Symfony \ Component \ Debug \ Exception \ FatalErrorException 

Call to undefined method stdClass::union() 

有關的任何想法? 謝謝!

P.S.文檔 - >http://laravel.com/docs/queries#unions

回答

3

當您撥打first(),您get()並返回第一個結果。因此,$gamer_id不再是查詢生成器,因此它缺少聯合功能。相反,請使用查詢修飾符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(); 
+0

是的,我只是看到了......謝謝ceejayoz –