Skip to content

Conversation

@amandiobm
Copy link
Contributor

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

<?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',
    ];
}

@jamesmills
Copy link
Owner

This is crazy amazing. Thank you @amandiobm.

@jamesmills
Copy link
Owner

wow!

*/
public function get($model, $key, $value, $attributes)
{
return TimezoneFacade::convertToLocal(Carbon::parse($value), $this->format, $this->showTimezone);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is returning a string, am I right? Assuming I am, I think that I'd still expect a Datetime or Carbon instance here, since it's usually what casting to datetime does. Next up might be doing a suggestion at the wrong place, and @jamesmills could jump in, but wouln't it be marvelous if casting to the right time zone returned a Carbon instance instead? That was we could use it's methods...

Doing so would make the package a drop in without big modifications into existing apps. For example, I have a couple of $model->created_at->diffForHumans() displayed here and there. Right now, if I decided to cast created_at => Timezone::class this will have me go over every instance of displaying a date to change it accordingly... And loose the functionnalities of Carbon.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually an interesting point. I'll give it some thought.

Copy link
Contributor Author

@amandiobm amandiobm May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpmurray adn @jamesmills Hey guys take a look into #35 .

@amandiobm
Copy link
Contributor Author

Close and moved to #35

@amandiobm amandiobm closed this May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants