By default
protected $dateFormat = 'Y-m-d H:i:s';
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In eloquent when storing any dates in the database as timestamt, in the database these fields have the form such as '2018-08-22 11:18:44'. When retrieve those dates and convert it to the JSON, those form not changed. Safari browser have a bug (e.g. Angular app) - the date pipe is crash app if date form differs from '2018-08-22T11:18:44' (need delimiter 'T' between date and time). In Eloquent i can define such property as $dateFormat, but any attempts to add delimiter 'T' is not success :(
Are there any suggestions to add a mutator of all dates for the model so that they do not go through them and replace them with regular expressions?
Dates are handled well in Laravel.
Add your field to the $dates array in the model.
Your field will then be converted to an instance of Carbon whenever the model is loaded.
Once it is a Carbon object it can be output in any format you like. There is really no complication to it.
If you need the date cast to a different format for a json response then see
https://laravel.com/docs/5.6/eloquent-mutators#date-casting
A carbon object may be converted to ISO format;
Tinker output;
Psy Shell v0.9.7 (PHP 7.2.9 — cli) by Justin Hileman
>>> $u=User::find(1)
=> App\User {#2954
id: 1,
name: "***************",
email: "***********@*******",
password: "y$qs1krUehz6QgWpAYCv2K8ecrXjp0jrLiA9h.UsC6JC/b4AVwAfblK",
remember_token: "8Iz7Xynwro4UNB4W1bX2RX9WRA5rNdUDX3N9lBpyHwErHuuWzA0BUqKg7rmF",
created_at: "2018-08-25 15:22:35",
updated_at: "2018-08-25 15:22:35",
}
//here you can see created_at is a carbon object
>>> $u->created_at
=> Illuminate\Support\Carbon @1535206955 {#2950
date: 2018-08-25 15:22:35.0 Europe/London (+01:00),
}
//cast to an ISO string
>>> $u->created_at->toIso8601String()
=> "2018-08-25T15:22:35+01:00"
>>>
here demonstrated with the User model and its created_at field
Please or to participate in this conversation.