I am trying to print some variables with these relations:
The first:
public function group(){
return $this->belongsTo(Group::class);
}
The second:
public function joinGroups(){
return $this->hasMany(JoinGroup::class);
}
My question is how could I access $group->joinGroups->name_group without doing a double foreach? I must do this that I show you below because the relationships are upside down...
@foreach ($groups as $group)
@foreach($group->joinGroups as $joinGroups)
{{$joinGroups->name_group}}
@endforeach
@endforeach
Is there an easier way to avoid the middle foreach? According to what code everything is duplicated.
CodePudding user response:
@foreach ($groups as $group)
{{$group->joinGroups[0]->name_group}}
@endforeach
you can apply the above code if you want to simply access the name_group of index 0, same goes for other index.