This tutorial shows how to cast your date fields in your models to a specific datetime format, enabling you to present data to end users in a specific style.
- [Narrator] In Laravel five point one,…we had the ability to cast a date,…and a datetime attribute of an eloquent model to a value…that we always want to deal with,…regardless of it's underlying format in the database.…Laravel five point six provides the ability…to cast the date and datetime strings…in particular formats.…We'll try this out now.…Open up the poll model at app, poll.php.…On line 15, before questions,…we can add a new property, casts.…
It's going to be a protected property, casts.…And this property is going to be an array.…Now this property, casts, defines the values…for which we are going to cast something to.…So for instance, on line 16,…we'll add the created at property…and we're going to cast it to a date string.…On the other hand, for the updated at property,…we're going to cast it to a datetime string.…
And now if we start our application…by running the command php artisan serve…and visit our homepage and our browser,…and then go to api/polls.…We'll see our polls, and notice…all of our created at fields have a time value…
Share this video
Embed this video
Video: Date casting