Atoagustyn's avatar

Many to Many relationship show columns of two different tables

I have two tables and I want to show all the columns in my view. This is the code for my models and controller. The foreach for $team->users returns null even though I have users in my table. Please what am I doing wrong?

public function users()
    {
        return $this->belongsToMany(User::class, ‘team_user', 'user_id', ‘team_id');
    }

public function teams()
    {
        return $this->belongsToMany(Team::class, 'team_user', 'user_id', 'team_id');
    }
  public function render()
    {
        $user = User::first();
        return view('livewire.team.index', compact('user'));
    }
<ul role="list" class="divide-y divide-gray-200">
                    @foreach ($user->team as $team)
                        <li>
                            <a href="#" class="block hover:bg-gray-50">
                                <div class="px-4 py-4 sm:px-6">
                                    <div class="flex items-center justify-between">
                                        <div class="truncate text-sm font-medium">
                                            {{ $team->name }}
                                        </div>
                                        @foreach ($team->users as $user)
                                            <p>{{ $user->name }}</p>
                                        @endforeach
                                    </div>
                                </div>
                            </a>
                        </li>
                    @endforeach
                </ul>
0 likes
6 replies
webrobert's avatar

the relationship name is teams

so you have to use 'teams'. not 'team'

@foreach ($user->teams as $team)

and you'll want to eager load the relations to prevent n+1 issues.

public function render()
{
   $user = User::with('teams.users')->first();
   return view('livewire.team.index', compact('user'));
}
Atoagustyn's avatar

@webrobert thanks for the eager loading tip and teams correction. I have fixed this but the users are not loading even though all the teams are loading.

Snapey's avatar

@Atoagustyn test in tinker

get a team model and then ask for $team->users

hopefully this code is in your Team model?

public function users()
{
    return $this->belongsToMany(User::class, ‘team_user', 'user_id', ‘team_id');
}

note that you have two incorrect quote marks here. This may be affecting the relationship definition

Atoagustyn's avatar

@Snapey I tried this and I get Property [users] does not exist on this collection instance.

newbie360's avatar
Level 24

@atoagustyn

public function users()
{
        return $this->belongsToMany(User::class, 'team_user', 'user_id', 'team_id');
}

should be

public function users()
{
        return $this->belongsToMany(User::class, 'team_user', 'team_id', 'user_id');
}

For less noise code, but i like keep the pivot table name

public function users()
{
        return $this->belongsToMany(User::class, 'team_user');
}

public function teams()
{
        return $this->belongsToMany(Team::class, 'team_user');
}

source code

public function belongsToMany(
    $related,
    $table = null,
    $foreignPivotKey = null,
    $relatedPivotKey = null,
    $parentKey = null,
    $relatedKey = null,
    $relation = null
)
1 like

Please or to participate in this conversation.