How can I echo Laravel table values in Many_to_Many relationship table

31 Views Asked by At

please I need help on this my Laravel project. So, I've a Many_to_Many relationship tables of users, group and group_user. In the system I'm working on, a user can create group by selecting a number of other registered users. My users table has id, first_name last_name and email columns, which I want to be able to access on my Group index page.

Group model

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

Users model

public function groups () {
     return this->belongsToMany(Group::class);
    } 

groupController

(...)->with('groups', Group::all())
->with('users', User::all()); 

group.blade.php

@foreach($groups as $group)
      @foreach($group->users as $user)
         {{$user->email}} 
      @endforeach  
    @endforeach 

I expect to see the email address of the user with the current id on the group page but it's not coming

0

There are 0 best solutions below