Created
June 7, 2020 13:19
-
-
Save jbrooksuk/677921edd58f87efedf1c5c803e7342f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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