Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dandefer's avatar

How to get data from 1:n raltion in blade ?

Hello,

I'm a beginner in Laravel, can you help me please ?

When the user is authentifate, I want to show his name in nav bar.

The problem is that I have 2 table (members and students), the name (first_name) is store in students table et the authentification proceed with members table.

So when I call, in blade, Auth()->user()->name or Auth()->user()->first_name, it's doesn't work..

It's possible to call a relation in blade view ?

0 likes
7 replies
catinodev's avatar

if you have have a member_id on the students table, in the model for the student you can do

public function member()
{
	return $this->belongsTo('App\Member');
}

then $member->student->first_name will work. However, it may be more viable to keep the first name information on the members table

Dandefer's avatar

Unfortunately, I create app from older app and I recovers existing tables.

I don't have member_id on the students table, but I have a student_id on the members table.

Can be work in this direction ?

piljac1's avatar
piljac1
Best Answer
Level 28

Then you can create a Student model if you don't already have one and in your User model you can add the following relation

public function student()
{
    return $this->belongsTo('App\Student');
}

And then if I understand correctly, your current User model uses the members table, so you can do :

auth()->user()->student->first_name

If you're not sure that the user will be authenticated or if the user (member) might not have a related student, you should use null coalescing to avoid an error.

auth()->user()->student->first_name ?? '' // Here the empty string is the default value if the user or the student isn't set.
Dandefer's avatar

Oh ok nice, it's work.

I had already try this but i had put hasOne like relation. I need to review the documentation.

Thanks you for your help !

piljac1's avatar

@dandefer Great ! Please consider marking the best answer to mark your issue as solved :)

piljac1's avatar

By the way, an easy way to remember the difference between hasOne and belongsTo :

A dog normally has a tag with information such as it's owner's name and/or phone number and/or address.

See this tag as the foreign key (owner_id).

We can then say that a dog belongs to its owner and the owner has one dog.

Of course this is really simplistic because an owner could have many dogs (hasMany).

Basically, the model having the foreign key in its table belongs to something.

1 like
ZhuYi's avatar

In Model Class, You can define relationship like this

public function student()
{
	return $this->belongsTo('App\Student');
}

And then In blade file, you can use this code for get first name.

@php
use Illuminate\Support\Facades\Auth;
@endphp
{{ is_object(Auth::guard('admin')->user()->student) ? Auth::guard('admin')->user()->student->first_name : "" }}

Please or to participate in this conversation.