3

I use Sentinel Framework to made authorization and authentication. But I stuck in User Management.

I want to show details like Email - Role but I still stuck here.

My Database:

role: ID, slug, name
user: ID, Email, ...
role_users: user_id, role_id

My Controller

  public function getAll(){
    $user = User::all();
    return view('admin.user.list',['user'=>$user]);
}

My UserModel

public function Role(){
    return $this->belongsToMany('App\Role','role_users','user_id', 'role_id');
}

Then my view

<?php foreach ($user as $u): ?>
<tr>
 <td>{{$u->Role->name}}</td>
</tr>
<?php endforeach; ?>

Then I got an error

Property [slug] does not exist on this collection instance. 

If I write

<td>{{$u->Role}}</td>

It's appears an array like

[{"id":1,"slug":"admin","name":"admin","permissions":null,"created_at":null,"updated_at":null,"pivot":{"user_id":1,"role_id":1}}]

But I can't access to the "slug" property :(

How can I do that ?

Thanks a lot !!!

Nguyen Hoang
  • 540
  • 5
  • 25

1 Answers1

0

user model

class User extends Model
    {
        /**
         * The roles that belong to the user.
         */
        public function roles()
        {
            return $this->belongsToMany('App\Role');
        }
    }

Role model

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users()
    {
        return $this->belongsToMany('App\User');
    }
}

controller

 public function getAll(){
    $user = User::all();
    return view('admin.user.list',['user'=>$user]);
}

view

@foreach ($user as $u)
    // becuase many to many you need this
   @foreach ($u->roles as $role) 
         {{$role->name}}
   @endforeach
@endforeach

check Laravel Many To Many Document for more info: Laravel Many To Many

Mortada Jafar
  • 3,529
  • 1
  • 18
  • 33
  • 1
    Hello Mortadda Jafar. Thanks about your help but it doesn't work. I still got an error ase table or view not found: 1146 Table 'shop.role_user' doesn't exist (SQL: select `roles`.*, `role_user`.`user_id` as `pivot_user_id`, `role_user`.`role_id` as `pivot_role_id` from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `role_user`.`user_id` = 1). I still read again Many to Many Document but still not find the answer :( Thanks for your help. – Nguyen Hoang May 01 '17 at 09:17
  • what is "shop.role_user"? – Mortada Jafar May 01 '17 at 09:59
  • role_user have 2 field is user_id and role_id. Here is my DB |role: ID, slug, name|; |user: ID, Email, ...|; |role_users: user_id, role_id| – Nguyen Hoang May 01 '17 at 10:45
  • please tell me what is database tables names ? – Mortada Jafar May 01 '17 at 10:47
  • Sorry for late reply. There are "users", "roles", "role_users". I using Sentinel Framework to made authorization and authentication. My database name is "shop". – Nguyen Hoang May 01 '17 at 18:10
  • Can you help me @Mortadda-Jafar ? – Nguyen Hoang May 03 '17 at 10:35
  • I can fix it right now. Because my table is not default so I just need to define in Mode liek: return $this->belongsToMany('App\User','role_users'); and it worked :D – Nguyen Hoang May 03 '17 at 14:43