51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Models\Casts;
|
||
|
|
||
|
use Carbon\Carbon;
|
||
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||
|
|
||
|
/**
|
||
|
* Cast into a Carbon DateTime instance
|
||
|
*/
|
||
|
class CarbonCast implements CastsAttributes
|
||
|
{
|
||
|
/**
|
||
|
* Transform the attribute from the underlying model values.
|
||
|
*
|
||
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||
|
* @param string $key
|
||
|
* @param mixed $value
|
||
|
* @param array $attributes
|
||
|
*
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function get($model, string $key, $value, array $attributes)
|
||
|
{
|
||
|
if ($value instanceof Carbon) {
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
return new Carbon($value);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Transform the attribute to its underlying model values.
|
||
|
*
|
||
|
* @param \Illuminate\Database\Eloquent\Model $model
|
||
|
* @param string $key
|
||
|
* @param mixed $value
|
||
|
* @param array $attributes
|
||
|
*
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function set($model, string $key, $value, array $attributes)
|
||
|
{
|
||
|
if ($value instanceof Carbon) {
|
||
|
return $value->toIso8601ZuluString();
|
||
|
}
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
}
|