I have 3 tables:
users (fileds: id, name, etc.) profiles (fields: id, user_id, first_name, last_name) orders (fields: id, user_id, etc.)
A user can have one profile / A profile can have one user A user can have many orders / An order can have one user
In the models I have:
User:
public function profile()
{
return $this->hasOne('App\Profile');
}
public function orders()
{
return $this->hasMany('App\Order');
}
Profile:
public function user()
{
return $this->belongsTo('App\User');
}
Order:
public function user()
{
return $this->hasOne('App\User');
}
Now, when I show an order in show.blade, I want to show the first_name and last_name columns from the profiles table.
How do I do that?
Thank you
You can load the relation and then use the object loaded
$order->load(['user.profile']);
$order->user->profile->first_name;
$order->user->profile->last_name;
or you can use query builder instead.
Thanks, but I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.order_id' in 'where clause' (SQL: select * from users
where users
.order_id
in (1))
Problems solved with help from stackoverflow:
http://stackoverflow.com/questions/43051342/laravel-5-4-struggling-with-relationships
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community