Support the ongoing development of Laravel.io →
Eloquent Views Blade

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

Last updated 3 years ago.
0

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.

0

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))

0

Sign in to participate in this thread!

Eventy

Your banner here too?

SigalZ sigalz Joined 5 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.