我的 DTO 类中有一个字段,它接受 start_time 和 end_time 作为“2:00 AM”
/**
* @var string
*/
#[CastWith(TimeCaster::class)]
public string $start_time; // 01:00 AM example
/**
* @var string
*/
#[CastWith(TimeCaster::class)]
public string $end_time;
Run Code Online (Sandbox Code Playgroud)
我可以在 Caster 类中使用 Carbon 解析这种时间格式吗
#[\Attribute] class TimeCaster implements Caster
{
public function cast(mixed $value): mixed
{
return Carbon::parse($value)->format();
}
}
Run Code Online (Sandbox Code Playgroud)