php Laravel 5.1:处理具有相同列名的连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33489406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1: handle joins with same column names
提问by Florian Lauterbach
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')
->get();
return $conversation;
}
It works fine so far, but the avatar's column name is 'name
' like the column name from the 'users
' table.
So if I'm using this query the to get the output via $conversation->name
, the avatar.name
overwrites the users.name
到目前为止它工作正常,但头像的列名是 ' name
' 就像来自 ' users
' 表的列名。因此,如果我使用此查询通过 获取输出$conversation->name
,则会avatar.name
覆盖users.name
Is there a way to rename the query output like the mysql "as" feature at laravel 5.1?
有没有办法像laravel 5.1中的mysql“as”功能一样重命名查询输出?
For example:
例如:
$conversation->avatarName
$conversation->userName
回答by Florian Lauterbach
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
正如你所提到的,我在 table.columnName 旁边添加了请求的“as-Feature”
回答by Chika Ugwuanyi
Take a look at this example of trying to join three tables staffs, customers and bookings(pivot table).
看看这个尝试加入三个表员工、客户和预订(数据透视表)的例子。
$bookings = \DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')
->with('bookings', $bookings);
回答by Toskan
I had the following problem, simplified example:
我有以下问题,简化示例:
$result = Donation::join('user', 'user.id', '=', 'donation.user_id')->where('user.email', '[email protected]')->first();
$result
is a collection of Donation
models. BUT CAREFUL:
$result
是Donation
模型的集合。但要小心:
both tables, have a 'created_at' column. Now which created_at
is displayed when doing $result->created_at
? i don't know. It seems that eloquent is doing an implicit select *
when doing a join, returning models Donation
but with additional attributes. created_at
seems random. So what I really wanted, is a return of all Donation
models of the user with email [email protected]
两个表都有一个“created_at”列。现在created_at
执行时显示哪个$result->created_at
?我不知道。似乎 eloquent 在进行select *
连接时做了隐含的操作,返回模型Donation
但具有附加属性。created_at
似乎是随机的。所以我真正想要的是Donation
用电子邮件返回用户的所有模型[email protected]
solution is this:
解决方案是这样的:
$result = Donation::select('donation.*')->join('user', 'user.id', '=', 'donation.user_id')->where('user.email', '[email protected]')->first();
回答by Yogesh kumar
Yeah, simply rename the column on either table and it should work. Also what you can do is, rename the user.name column to anything, also rename sender column of conversation_messages to id and perform a natural join.
是的,只需重命名任一表上的列,它应该可以工作。您还可以做的是,将 user.name 列重命名为任何内容,还将 session_messages 的 sender 列重命名为 id 并执行自然连接。