How to avoid double foreach in backward relationships in Laravel?

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

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.

‫أضف إجابة

تصفح
تصفح

مجهول يجيب