php - Table joins in Laravel -
database structure
users
id | name ---+--------- 1 | op 2 | noob
items
id | name | user_id ---+------+-------- 1 | car | 1 2 | bus | 2 3 | box | 1
comments
id | user_id | item_id | comment ---+---------+---------+--------------- 1 | 1 | 3 | box.
app
app/routes.php
view::share('top_comments', user::join('comments', 'users.id', '=', 'comments.user_id') ->take(3) ->get() );
app/views/test.php
@foreach ($top_comments $comment) username: {{ $comment->name }}<br /> comment: {{ $comment->comment }}<br /> item id: {{ $comment->item_id }} @endforeach
current output
username: op comment: box item id: 3
desired output
username: op comment: box item id: 3 item name: box
i want able adjust top_comments
query include items.name
. {{ $comment->name }}
returns user's name. think having same field name causing conflict... suggestions?
you may use this:
user::join('comments', 'users.id', '=', 'comments.user_id') ->join('items', 'users.id', '=', 'items.user_id') ->get(array('users.*', 'comments.*', 'items.user_id', 'items.name itemname'));
in view
:
@foreach ($top_comments $comment) ... item name: {{ $comment->itemname }} @endforeach
Comments
Post a Comment