Simple Date Accessors – Laravel

For scenarios where you require your dates in Laravel to be properly formatted in human readable form for your APIs. Here's the simple snippet you can use in your Model class.

Reading Time: < 1 minute
/**
 * @return string
 */
public function getCreatedAtAttribute()
{
    return Carbon::parse($this->attributes['created_at'])->diffForHumans();
}

/**
 * @return string
 */
public function getUpdatedAtAttribute()
{
    return Carbon::parse($this->attributes['updated_at'])->diffForHumans();
}

You can learn more about Laravel Accessors here.

Comments