Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk
Created June 7, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrooksuk/677921edd58f87efedf1c5c803e7342f to your computer and use it in GitHub Desktop.
Save jbrooksuk/677921edd58f87efedf1c5c803e7342f to your computer and use it in GitHub Desktop.
<?php
namespace App\Casts;
use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class LocalizedDateTime implements CastsAttributes
{
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return array
*/
public function get($model, $key, $value, $attributes)
{
if (! $value) {
return null;
}
return (new Carbon($value))->tz(auth()->user()->timezone ?? $model->timezone ?? 'UTC');
}
/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param array $value
* @param array $attributes
* @return string
*/
public function set($model, $key, $value, $attributes)
{
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment