argument('name'); $userId = $this->argument('userId'); $guardName = $this->argument('guard'); $userModelClass = $this->argument('userModelNamespace'); // Validate that the model class exists and is instantiable if (! class_exists($userModelClass)) { $this->error("User model class [{$userModelClass}] does not exist."); return Command::FAILURE; } $user = (new $userModelClass)::find($userId); if (! $user) { $this->error("User with ID {$userId} not found."); return Command::FAILURE; } /** @var \Spatie\Permission\Contracts\Role $roleClass */ $roleClass = app(RoleContract::class); $role = $roleClass::findOrCreate($roleName, $guardName); $user->assignRole($role); $this->info("Role `{$role->name}` assigned to user ID {$userId} successfully."); return Command::SUCCESS; } }