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