<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;
class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class,
];
}
Advanced usage
Custom format
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;
class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':Y-m-d H:i:s',
];
}
Return the timezone as a string passing along the format.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;
class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':d/m/Y H:i:s,true',
];
}
Return the timezone as a string using the default format.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use JamesMills\LaravelTimezone\Casts\Timezone;
class Foo extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'created_at' => Timezone::class.':null,true',
];
}
Laravel gives us the ability to use custom classes when casting attributes directly from the model.
Docs (https://laravel.com/docs/7.x/eloquent-mutators#custom-casts)
Basic usage
Advanced usage
Custom format
Return the timezone as a string passing along the format.
Return the timezone as a string using the default format.