用户
id | name
---+---------
1 | OP
2 | noob
项
id | name | user_id
---+------+--------
1 | car | 1
2 | bus | 2
3 | box | 1
评论
id | user_id | item_id | comment
---+---------+---------+---------------
1 | 1 | 3 | this is a box.
应用/ routes.php文件
View::share('top_comments', User::join('comments', 'users.id', '=', 'comments.user_id')
->take(3)
->get()
);
应用/视图/ test.php的
@foreach ($top_comments as $comment)
username: {{ $comment->name }}<br />
comment: {{ $comment->comment }}<br />
item id: {{ $comment->item_id }}
@endforeach
当前输出
username: OP
comment: this is a box
item id: 3
所需的输出
username: OP
comment: this is a box
item id: 3
item name: box
我希望能够调整top_comments
查询以包含items.name
。目前{{ $comment->name }}
返回用户名。我认为拥有相同的字段名称会导致冲突...建议?
答案 0 :(得分:0)
你可以用这个:
User::join('comments', 'users.id', '=', 'comments.user_id')
->join('items', 'users.id', '=', 'items.user_id')
->get(array('users.*', 'comments.*', 'items.user_id', 'items.name as itemname'));
在view
:
@foreach ($top_comments as $comment)
...
item name: {{ $comment->itemname }}
@endforeach