如何在laravel 4.2中分配角色

There are 2 roles: 1. Admin 2. users in the project.

How can I assign roles when a user is registering ?

I have 3 tables

  • users - user_id
  • roles - role_id
  • user_roles - userid, roleid

Here is the code for hasrole - whether particular role exits.

public function hasRole($key) {
       //  return in_array($check, array_fetch($this->roles->toArray(), 'name'));
    foreach ($this->roles as $role) {
        if ($role->ROLE_NAME == $key) {
            return true;
        }
    }

    return false;
 }