$rules['memo'], 'allowed_ips' => $rules['allowed_ips'], 'allowed_ips.*' => $rules['allowed_ips.*'], ]; } public function create(User $user, ?string $description, ?array $allowedIps): NewAccessToken { $validator = Validator::make(['description' => $description, 'allowed_ips' => $allowedIps], self::rules()); $validator->after(fn () => self::validateAllowedIps($validator)); $description = $validator->safe()->string('description')->toString(); $token = DB::transaction(function () use ($user, $description, $allowedIps): NewAccessToken { $user = User::query()->lockForUpdate()->findOrFail($user->id); throw_if($user->apiKeys()->count() >= 25, DisplayException::class, 'You have reached the account limit for number of API keys.'); return $user->createToken($description, $allowedIps); }); Activity::event('user:api-key.create') ->subject($user, $token->accessToken) ->property('identifier', $token->accessToken->identifier) ->log(); return $token; } }