*/ #[ResponseField('max_connections', 'integer', example: 0, nullable: true)] class DatabaseResource extends ClientResource { protected array $eagerLoads = ['host', 'server.subusers']; protected array $availableIncludes = ['password']; public function resourceName(): string { return Database::RESOURCE_NAME; } public function toArray(Request $request): array { $this->resource->loadMissing('host'); $host = $this->resource->host; return [ 'id' => Hashids::encode($this->resource->id), 'host' => [ 'address' => $host->host, 'port' => $host->port, ], 'name' => $this->resource->database, 'username' => $this->resource->username, 'connections_from' => $this->resource->remote, 'max_connections' => $this->resource->max_connections, ]; } protected function resolveInclude(string $include): ?IncludedResource { return match ($include) { 'password' => $this->includePassword(), default => parent::resolveInclude($include), }; } /** * @return ApiResource|NullResource */ private function includePassword(): ApiResource|NullResource { if (! $this->getUser()->can(Permissions::DatabaseViewPassword->value, $this->resource->server)) { return $this->null(); } return $this->item($this->resource, fn (Database $model): array => [ 'password' => Crypt::decrypt($model->password), ], 'database_password'); } }