$r->role->name
Have you defined a role relationship?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi guys,
First, apologizes if my question has already been asked many times. I have three tables: Referees, Match and Roles. Theses tables are linked with a Many to Many relationship like this :
public function referees()
{
return $this->belongsToMany('App\Referee', 'leads');
}
And
public function matches()
{
return $this->belongsToMany('App\Match', 'leads');
}
On matches index page, I want to retrieve the list of matches with each referees and their roles for each games. I tried this :
@if(!$match->referees->isEmpty())
<ul>
@foreach($m->referees as $r)
<li>{{ $r->first_name }} {{ $r->name }} : {{ $r->role->name}}</li>
@endforeach
</ul>
@else
-
@endif
But it gives me this error and I can't understand why:
Trying to get property 'name' of non-object
Could you please help me ?
Please or to participate in this conversation.