n How do I change the date format Laravel outputs to JSON | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this post, I'll show you How do I change the date format Laravel outputs to JSON.

Example 1: By overriding the serializeDate method in your model:

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

 

Example 2: you can customize The Date Format Per Attribute  by specifying the date format in the cast declaration: like this:

protected $casts = [
    'event_date' => 'date:Y-m-d',
    'created_at' => 'datetime:Y-m-d H:00',
];

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook