*/ class UtcDatetimeCast implements CastsAttributes { public bool $withoutObjectCaching = true; /** @param array $attributes */ #[\Override] public function get(Model $model, string $key, mixed $value, array $attributes): ?Carbon { if ($value === null || $value === '') { return null; } return $this->toUtcCarbon($value); } /** @param array $attributes */ #[\Override] public function set(Model $model, string $key, mixed $value, array $attributes): ?string { if ($value === null || $value === '') { return null; } return $this->toUtcCarbon($value)->format($model->getDateFormat()); } private function toUtcCarbon(mixed $value): Carbon { if ($value instanceof \DateTimeInterface) { return Date::instance($value)->setTimezone('UTC'); } if (is_int($value)) { return Date::createFromTimestamp($value, 'UTC'); } if (is_string($value)) { return Date::parse($value, 'UTC')->setTimezone('UTC'); } throw new \InvalidArgumentException('The given value cannot be cast to a UTC datetime.'); } }