Laravel 4.2 union

与世无争的帅哥 提交于 2019-12-24 02:33:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!