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',
];

 

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon