Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!