Skip to content

Commit 66d709e

Browse files
author
Amandio Magalhaes
committed
Returning Carbon instances when using Model Casting
1 parent e202cd3 commit 66d709e

File tree

4 files changed

+94
-11
lines changed

4 files changed

+94
-11
lines changed

README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Laravel Timezone
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
4-
[![Total Downloads](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
5-
[![Licence](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone)
6-
[![Quality Score](https://img.shields.io/scrutinizer/g/jamesmills/laravel-timezone.svg?style=flat-square)](https://scrutinizer-ci.com/g/jamesmills/laravel-timezone)
7-
[![StyleCI](https://github.styleci.io/repos/142882574/shield?branch=master)](https://github.styleci.io/repos/142882574)
8-
[![Buy us a tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone)
9-
[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone)
3+
[![Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=for-the-badge)](https://packagist.org/packages/jamesmills/laravel-timezone)
4+
![Packagist](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=for-the-badge)
5+
![Packagist](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=for-the-badge)
6+
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone)
7+
[![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=for-the-badge)](https://plant.treeware.earth/jamesmills/laravel-timezone)
108

119
An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone.
1210

@@ -90,6 +88,31 @@ And with custom formatting
9088
// 2018-07-04 3:32 New York, America
9189
```
9290

91+
### Using models casting class
92+
93+
#### Basic usage
94+
95+
```
96+
<?php
97+
98+
namespace App;
99+
100+
use Illuminate\Database\Eloquent\Model;
101+
use JamesMills\LaravelTimezone\Casts\Timezone;
102+
103+
class Foo extends Model
104+
{
105+
/**
106+
* The attributes that should be cast to native types.
107+
*
108+
* @var array
109+
*/
110+
protected $casts = [
111+
'created_at' => Timezone::class,
112+
];
113+
}
114+
```
115+
93116
### Saving the users input to the database in UTC
94117

95118
This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance.

src/Casts/Timezone.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace JamesMills\LaravelTimezone\Casts;
4+
5+
use Carbon\Carbon;
6+
use Exception;
7+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8+
use Illuminate\Database\Eloquent\Model;
9+
use JamesMills\LaravelTimezone\Facades\Timezone as TimezoneFacade;
10+
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;
11+
12+
class Timezone implements CastsAttributes
13+
{
14+
use TimezoneTrait;
15+
/**
16+
* Transform the attribute from the underlying model values.
17+
*
18+
* @param Model $model
19+
* @param string $key
20+
* @param mixed $value
21+
* @param array $attributes
22+
* @return Carbon
23+
* @throws Exception
24+
*/
25+
public function get($model, string $key, $value, array $attributes)
26+
{
27+
return Carbon::parse($value)
28+
->setTimezone($this->getUserTimezone());
29+
}
30+
31+
/**
32+
* Transform the attribute to its underlying model values.
33+
*
34+
* @param Model $model
35+
* @param string $key
36+
* @param mixed $value
37+
* @param array $attributes
38+
* @return Carbon
39+
*/
40+
public function set($model, string $key, $value, array $attributes)
41+
{
42+
return TimezoneFacade::convertFromLocal($value);
43+
}
44+
}

src/Timezone.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace JamesMills\LaravelTimezone;
44

55
use Carbon\Carbon;
6+
use JamesMills\LaravelTimezone\Traits\TimezoneTrait;
67

78
class Timezone
89
{
10+
use TimezoneTrait;
11+
912
/**
1013
* @param Carbon|null $date
1114
* @param null $format
@@ -18,9 +21,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
1821
return 'Empty';
1922
}
2023

21-
$timezone = (auth()->user()->timezone) ?? config('app.timezone');
22-
23-
$date->setTimezone($timezone);
24+
$date->setTimezone($this->getUserTimezone());
2425

2526
if (is_null($format)) {
2627
return $date->format(config('timezone.format'));
@@ -41,7 +42,8 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone =
4142
*/
4243
public function convertFromLocal($date) : Carbon
4344
{
44-
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
45+
return Carbon::parse($date, auth()->user()->timezone)
46+
->setTimezone('UTC');
4547
}
4648

4749
/**

src/Traits/TimezoneTrait.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace JamesMills\LaravelTimezone\Traits;
4+
5+
trait TimezoneTrait
6+
{
7+
/**
8+
* @return string
9+
*/
10+
protected function getUserTimezone(): string
11+
{
12+
return (auth()->user()->timezone) ?? config('app.timezone');
13+
}
14+
}

0 commit comments

Comments
 (0)