I recently upgraded from Laravel 6 to V7, and I have a Vue component that is responsible for displaying the log in the following date format: YYYY-MM-DD H:i:s / 2021-10-08 18:28:32
So up to that point it worked great with v6, and until today it started showing me the date like this: 2021-10-04T18:47:33.000000Z .
It is correct but I only use up to the seconds, not the time zone, I use the date format of momentjs but apparently it does not respect it and it is printed with the 0;
I thought it was the moment.js and I checked the documentation and it agrees with the documentation, so the only change I made was to Laravel v7.
Also check in the database and it is inserted as is YYYY-MM-DD H:i:s the point is when it enters the Laravel Query, and processes the data:
You receive them like this: 2021-10-08 18:28:32
$timeline = $user->elogbook()->orderBy('updated_at', 'desc')->get();;
And it returns them like this: 2021-10-08T18:27:32.000000Z
I found that to fix it you had to add this in the model:
$date->format('Y-m-d H:i:s');
I tried it but it still appears the same, some alternative or model example in case I add it wrong:
Model User function:
public function elogbook() {
return $this->hasMany(Logbook::class, 'executor_id');
}
Model Timelog function
public function executor() {
return $this->belongsTo(User::class, 'executor_id');
}
Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models.
Previously, dates would be serialized in a format like the following:
Dates serialized with the new format will appear as:
Note that ISO-8601 dates are always expressed in UTC.
If you want to keep using the old behavior, you can override the serializeDate method on your model:
Enter the above snippet in the models where you want the date to appear ( 2019-12-02 20:01:00. ) in that format, that way it will override date serialization after the new update.
More info here in the documentation