Language:

Search

Simple Date Accessors - Laravel

  • Share this:
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.

/**
 * @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.

Usama Muneer

Usama Muneer

A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.