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

massartval's avatar

How can I display a property from a different model in my template?

I am building an app that handles courses, study groups and students. Courses have a one-to-many relationship with groups, and groups have a many-to-many relationship with profiles.

Right now I am building the user dashboard. This dashboard shows the groups that the authenticated user is a part of. I would like to display the names of the other students that are part of each group. The method that gets the user-related groups is the following.

    public function show()
    {
        $groups = auth()->user()->profile->isStudentInGroup()->orderBy('course_id')->get();
        return view('dashboard', compact('groups'));
    }

In my dashboard.blade.php, I can display the ids of the other students like this :

@foreach($groups as $group)
    <div>
        {{ $group->course->name }} {{ $group->teacher_name }} {{ $group->weekday }} {{ Carbon::parse($group->start_time)->format('H:i') }}
    </div>
    <div>
        @foreach($group->hasStudents as $student) 
        {{ $student->id }}
        @endforeach
    </div>
@endforeach

As I said, I would like to display their names instead of their ids. I can't access that property from the template file because my hasStudents() relationship only stores the group_id and the profile_id. I would also like to have a groups.index route for the admin, and I would like to reuse that functionality there. So I have the intuition that I should not write that logic inside of my dashboard controller.

What would be a good design to achieve that? Should I make my groups list into a component and bring the names to the component via the component class? Is there a better or cleaner way to do it?

Thanks in advance.

EDIT :

I have found a way to display the names but it's very bad. I welcome all feedback.

In my controller :

    public function show()
    {
        $groups = auth()->user()->profile->isStudentInGroup()->orderBy('course_id')->get();
		$profiles = Profile::get();
        return view('dashboard', compact('groups', 'profiles'));
    }

In my view :

@foreach($groups as $group)
    <div>
        {{ $group->course->name }} {{ $group->teacher_name }} {{ $group->weekday }} {{ Carbon::parse($group->start_time)->format('H:i') }}
    </div>
    <div>
        @foreach($group->hasStudents as $student) 
        {{ $student->id }} {{ $profiles[$student->id -1]->user->name }}
        @endforeach
    </div>
@endforeach
0 likes
6 replies
tykus's avatar
tykus
Best Answer
Level 104

can't access that property from the template file because my hasStudents() relationship only stores the group_id and the profile_id

It would seem that this relationship has not been properly defined. You have, perhaps, a HasMany relationship where a BelongsToMany was needed.

// Group
public function profiles()
{
    return $this->belongsToMany(Profile::class);
}

Then in the view, assuming Profile model and this student loop variable are then same thing:

@foreach($groups as $group)
    <div>
        {{ $group->course->name }} {{ $group->teacher_name }} {{ $group->weekday }} {{ Carbon::parse($group->start_time)->format('H:i') }}
    </div>
    <div>
        @foreach($group->profiles as $profile) 
        {{ $profile->name }}
        @endforeach
    </div>
@endforeach
1 like
massartval's avatar

@tykus My relationships were ok, but your comment made me understand what I was overlooking. I thought that I needed to bring some more data because the name variable is stored with the User, not with the Profile. I forgot that Laravel is very smart and I didn't even try the most obvious solution.

All I needed to do was this:

        @foreach($group->hasStudents as $student) 
        {{ $student->user->name }} 
        @endforeach
tykus's avatar

@massartval it is not possible for us to understand anything about your application except what you have shared (it is easily when conventions are followed, such as relationship names). So, exact solutions can be difficult.

Anyway, you seem to have solved your problem

massartval's avatar

@tykus I didn't mean to discard your input, it was actually very helpful!

The relationships between the models Group and Profile are named as follows :

  • Profile : isStudentInGroup()
  • Group : hasStudents() Are those bad names?
tykus's avatar

@massartval for me, they don't communicate their intent. is and has are closed questions; I'd expect a boolean return value from those methods.

1 like
massartval's avatar

@tykus Oh... I think I understand now.

stackoverflow.com/questions/37403161/laravel-eloquent-relationship-many-to-many-naming-convention

I will rename them groups() and profiles().

Please or to participate in this conversation.