6

I have a query like this:

$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get();

and a table that has a column id (users.id) and another table that has columns id and user_id (user_roles.id & user_roles.user_id),

but the problem is.. what is being returned on $user->id is the user_roles.id instead of the users.id column.. how do i fix this so that what i get is not the role id but the user id instead..

thanks!

2 Answers 2

16

Found it!

using ->select('users.*', 'user_roles.role_name') i was able to remove user_roles.id from the returned values and thus eliminating the conflict.

Here is the final query:

$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->select('users.*', 'user_roles.role_name')->get();
1
  • 2
    an alternative although poor solution is ->select('*', 'users.id')
    – cawecoy
    Jul 31, 2014 at 18:35
6

The better way to do this is using 'as':

$users = DB::table('users')->join('user_roles','users.role_id','=','user_roles.id')->get(array('users.*', '**user_roles.id as user_roles_id**', 'user_roles.*'));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.