26
votes

I have in model:

protected $casts = [
    'date' => 'date',
];

Does laravel have some ability to set cast format, something like:

protected $casts = [
    'date' => 'date_format:d/m/yyyy',
];

?

EDITED

I tried this:

In model:

protected $dateFormat = 'm/d/Y';

protected $dates = ['driver_expiration', 'created_at', 'updated_at', 'deleted_at'];

protected $casts = [
    'driver_expiration'     => 'date',
];

I saved date(driver_expiration) as '01/012016' but date is saved 0000-00-00.

laravel documentation: https://laravel.com/docs/5.1/eloquent-mutators Tell us $dateFormat works only for timestamps ('created_at', 'updated_at', 'deleted_at')

5

5 Answers

71
votes

After laravel 5.6,

You can individually customize the format of Eloquent date and datetime casting:

protected $casts = [
    'birthday'  => 'date:Y-m-d',
    'joined_at' => 'datetime:Y-m-d H:00',
];

This format is used when the model is serialized to an array or JSON data

38
votes

Instead of casting a date format, you can use mutators to set the format you like:

For example, if your column was called date, you can add the following function to your model:

public function setDateAttribute( $value ) {
  $this->attributes['date'] = (new Carbon($value))->format('d/m/y');
}

The setDateAttribute is always in the format set[ColumnName]Attribute, using CamelCase. The date in the $this->attributes['date'] part must also match your actual column name in the table.

When you create or update a record in your database, the format will automatically be changed by the function above.

Note: You may need to add the use Carbon\Carbon statement to the top of your Model to use the Carbon library.

Take a look at other examples here.


UPDATE

Optionally, you can define a format for date columns using:

protected $dateFormat = 'Y-m-d';
5
votes

Just add to the model:

protected function asDateTime($value)
{
    return parent::asDateTime($value)->format('d/m/y');
}

this Eloquent's method originally return a Carbon instance, so you can get that instance and calls to its format() method. If you wish to apply this solution to all models, just create a new class extended from Eloquent, and put this method there, so then you can extend the models from this new class.

2
votes

I create new model property:

protected $datesConvert = ['driver_expiration'];

And then I updated my base model like :

public function save(array $options = [])
{
    if(isset($this->datesConvert)){
        foreach($this->datesConvert as $date){
            $this->attributes[$date] = \Carbon\Carbon::createFromFormat('d/m/Y', $this->attributes[$date])->format('Y-m-d');
        }
    }

    parent::save($options);

}

public function getAttribute($key)
{
    $value = parent::getAttribute($key);

    if(isset($this->attributes[$key])){
        if(isset($this->datesConvert)  &&  in_array($key, $this->datesConvert)){
            $value = \Carbon\Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y');
        }

    }

    return $value;
}

and this solution works for me. Code can be moved to trait. Format can be put in $datesConvert Format = 'd/m/Y';

0
votes

Casts are not affected by the Collection object. It's used for json and array formats. If you need an individual cast that you can use

 $user->date->format('m-d-Y');