Florian Lauterbach Florian Lauterbach - 3 months ago 21x
SQL Question

Laravel 5.1: handle joins with same column names

I'm trying to fetch following things from the database:

  • user name

  • user avatar_name

  • user avatar_filetype

  • complete conversation_messages

with the following query:

static public function getConversation($id)
$conversation = DB::table('conversation_messages')
->where('belongsTo', $id)
->join('users', 'conversation_messages.sender', '=', 'users.id')
->join('user_avatars', 'conversation_messages.sender', '=', 'user_avatars.id')
->select('users.name', 'conversation_messages.*', 'user_avatars.name', 'user_avatars.filetype')
return $conversation;

It works fine so far, but the avatar's column name is '
' like the column name from the '
' table.
So if I'm using this query the to get the output via
, the
overwrites the

Is there a way to rename the query output like the mysql "as" feature at laravel 5.1?

For example:




Meh okay.. i've found a simple solution here

->select('users.name as userName', 'conversation_messages.*', 'user_avatars.name as avatarName', 'user_avatars.filetype')

As you can mention I've added the requested "as-Feature" next to the table.columnName