Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

duncanhiggons's avatar

Error assigning role: There is no role named `Admin`

I can't get roles to be able to be assigned to users.

My function that assigns user roles is as below, but it returns the following error - [2023-06-26 17:59:58] local.ERROR: Error assigning role: There is no role named Admin.

This is my laravel log

[2023-06-26 17:59:58] local.INFO: Data received: {"role":"Admin","users":["1"]} [2023-06-26 17:59:58] local.DEBUG: Request details: {"data":{"role":"Admin","users":["1"]}} [2023-06-26 17:59:58] local.INFO: Available Roles: Admin, Admin Manager, Academic Manager, Technical Manager, Admin Assistant, Academic Assistant, Instructional Designer, Grader, Teacher, Learner, Guest Learner, Blogger [2023-06-26 17:59:58] local.INFO: User: [{"id":1,"name":"duncan","email":"[email protected]","current_team_id":1,"email_verified_at":null,"two_factor_confirmed_at":null,"created_at":"2023-05-27T20:23:55.000000Z","updated_at":"2023-06-15T20:15:19.000000Z","profile_photo_path":"profile-photos/1686187515.png","first_name":"Duncan","surname":"Higgons","dob":"1985-01-22","gender":"Male","state":"Bogota","country":"United Kingdom","languages":"English","job_title":"Founder","company":"Lengua","phone":"1321","document_id":"123","id_type":"C\u00e9dula de extranjer\u00eda","locale":"es","profile_photo_url":"http://localhost/storage/profile-photos/1686187515.png"}] [2023-06-26 17:59:58] local.INFO: roleName: Admin [2023-06-26 17:59:58] local.INFO: teamId: 1 [2023-06-26 17:59:58] local.INFO: Guard: web [2023-06-26 17:59:58] local.INFO: Role: Admin [2023-06-26 17:59:58] local.INFO: User Guard: web [2023-06-26 17:59:58] local.ERROR: Error assigning role: There is no role named Admin.

As you can see Admin is in the roles list, it exists in the database table, I've tried debugging lots of ways but can't find any solution that works

public function assignRole(Request $request) {

Log::info('Data received: ', $request->all());
\Log::debug('Request details:', ['data' => $request->all(),]);
$roles = Role::all();
Log::info('Available Roles: ' . $roles->pluck('name')->implode(', '));

try {
    $users = User::find($request->users);
    Log::info('User: '. $users);
    $roleName = $request->role;
    Log::info('roleName: '. $roleName);
    $teamId = 1; // default team id for general roles
    Log::info('teamId: '. $teamId);

    $guard = 'web'; // specify your guard name here
    Log::info('Guard: '. $guard);

    $role = Role::where(['name' => $roleName, 'guard_name' => $guard])->first();
    Log::info('Role: '. $role->name);

    $users->each(function ($user) use ($role, $teamId) {
        $userGuard = $this->getGuardName($user);
        Log::info('User Guard: '. $userGuard);
        if ($userGuard === $role->guard_name) {
            $user->assignRole($role->name, $teamId);

        } else {
            \Log::error('Error assigning role: User guard and role guard do not match.', ['user' => $user, 'role' => $role]);
        }
    });

    if ($role) {
        Log::info('Role: '. $role);

        \Log::debug('Fetched role:', ['role' => $role,]);

        $users->each(function ($user) use ($role, $teamId) {
            $user->assignRole($role->name, $teamId);
        });

        return response()->json(['message' => 'Role assigned successfully'], 200);
    } else {
        \Log::error('Error assigning role: Role not found.');
        return response()->json(['error' => 'Role not found'], 404);
    }
} catch (\Exception $e) {
    \Log::error('Error assigning role: ' . $e->getMessage());
    return response()->json(['error' => 'Error assigning role'], 500);
}

}

0 likes
4 replies
Snapey's avatar

when you log available roles, also include the guard?

duncanhiggons's avatar

@Snapey They all have the web guard

[2023-06-27 16:17:41] local.INFO: Available Roles: Admin (web), Admin Manager (web), Academic Manager (web), Technical Manager (web), Admin Assistant (web), Academic Assistant (web), Instructional Designer (web), Grader (web), Teacher (web), Learner (web), Guest Learner (web), Blogger (web)

Snapey's avatar

what if you change this line

$user->assignRole($role->name, $teamId);

$user->assignRole($role, $teamId);

Please or to participate in this conversation.