Dates Not Casting After Upgrading to Laravel 7

Dates not casting after upgrading to Laravel 7

Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models.

Previously, dates would be serialized to a format like the following :

2019-12-02 20:01:00

Dates serialized using the ISO-8601 format will appear like :

2019-12-02T20:01:00.283041Z

Please note that ISO-8601 dates are always expressed in UTC.

If you would like to keep using the previous behavior you can override the serializeDate() method on your model :

use DateTimeInterface;

protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}

See the official upgrade doc here

Laravel casts set date format not working

The casting is done when you convert the model to an array or json format.

class Booking extends Model
{
use HasFactory;
protected $casts = [
'time_from' => 'datetime:m-d-Y',
];
}
App\Models\Booking::first()->time_from

=> Illuminate\Support\Carbon { ... }
App\Models\Booking::first()->toArray()['time_from'] 

=> '01-02-2021'
App\Models\Booking::first()->toJson()

=> "{... "time_from":"01-02-2021", ....}"

Laravel: how to set date format on model attribute casting?

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';

Laravel shows wrong date when casting from timestamp to date:Y/n/j

Since Laravel 7.0, when calling toJson the framework uses Carbon's toJson method behind the scenes which produces ISO-8601 compatible dates which are always expressed in UTC. Therefore, I'm guessing your timezone is converted to UTC before the date is casted to your required format and thus something like this is happening:

\Carbon\Carbon::parse('2021-06-08 01:00:01', 'Asia/Tokyo')->timezone('UTC')->format('Y/n/j'); // 2021/6/7
\Carbon\Carbon::parse('2021-06-08 08:59:59', 'Asia/Tokyo')->timezone('UTC')->format('Y/n/j'); // 2021/6/7
\Carbon\Carbon::parse('2021-06-08 09:00:00', 'Asia/Tokyo')->timezone('UTC')->format('Y/n/j'); // 2021/6/8

I suggest you to keep storing and returing everything in UTC and converting to the required timezone at the last possible moment to keep everything managable. Although, if you insist on storing everything in Asia\Tokyo and returning it in the same timezone and doing no conversions, then overriding serilizeDate function for your models should do the trick as described in an earlier upgrade guide:

Laravel 7.0 upgrade guide: Date Serialization

Laravel why my User model is automatically converting unix timestamp to datetime string

Since Laravel 7, they have changed the date serialization to Carbon's toJson method as stated in their upgrade guide. To revert it, add a serializeDate method to your models.

use DateTimeInterface;

/**
* Prepare a date for array / JSON serialization.
*
* @param \DateTimeInterface $date
* @return string
*/
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}

Changing Date format through cast and date property of laravel not working on server

Ok, So after checking for every error on earth, I have found that the issue was, in my local I was using laravel 5.6 but on my server I was using laravel 5.5 . So basically it was all laravel version issue.
This trick is definitely best for those who wants dates field in the desired format without changing the original format.

laravel get data without applying casts

You can get all attributes as they are, by using

Model::get()->transform(function ($item) {
return $item->getOriginal();
}))->toArray();

Also can use getOriginal() as

$model->getOriginal('created_at') 

on any model to get the original value whenever it's needed.

Note : getOriginal() will include all the $hidden attributes of the model.



Related Topics



Leave a reply



Submit