Also: StoresUtc trait normalizes datetime casts to UTC (controller ruling). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
24 lines
689 B
PHP
24 lines
689 B
PHP
<?php
|
|
|
|
namespace App\Models\Concerns;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use DateTimeInterface;
|
|
|
|
/**
|
|
* Eloquent форматирует datetime без смещения, поэтому Carbon в чужом поясе
|
|
* записался бы как «те же цифры в UTC». Приводим к UTC при присвоении.
|
|
*/
|
|
trait StoresUtc
|
|
{
|
|
public function setAttribute($key, $value)
|
|
{
|
|
if ($value instanceof DateTimeInterface
|
|
&& in_array($this->getCasts()[$key] ?? null, ['datetime', 'immutable_datetime'], true)) {
|
|
$value = CarbonImmutable::instance($value)->utc();
|
|
}
|
|
|
|
return parent::setAttribute($key, $value);
|
|
}
|
|
}
|