how to communicate between client & server with ISO8601, but store in standard date format, with minimal project modifications?
By default, Laravel will cast/serialize/access/mutate dates as "Standard date format", also known as 'Y-m-d H:i:s'. This is fine, it works well for interacting with MySQL, as it's—I believe—the standard data storage format for the he `DATE column type.
However, my client is built using Angular, which often expects dates to be parsed as ISO8601, and in fact, the built in date and corresponding formatDate pipes Angular provides don't work out of the box with "Standard date formats" that Laravel expects at all. JavaScript's built in Date object also serialises better to ISO8601 than Laravel's preferred date format.
From this, it appears the quickest solution to this is to force Laravel to send all dates as ISO8601 from the API, process all received dates as ISO8601 back to 'Y-m-d H:i:s' and internally store them in my database as its preferred format.
Where I'm getting stuck is on how I can achieve this with minimal modifications to my Laravel project, from a vanilla installation. I'm not after a solution which requires implementing multiple mutators/accessors on the dozens of different dates on the dozens of different models I have.
There's also a huge number of different date manipulation options that Laravel provides, that I frankly can't get working in the correct order. Off the top of my head, there's:
- Model Accessors/Model Mutators.
-
Date Serialization in
AppServiceProvider - Date Mutators
-
$protected $dateFormat - Date Casting
What modifications do I need to make to my Laravel project to enable the outcome I'm looking for?
Please or to participate in this conversation.