Search

Programming
Programming

We provide developers and engineers the guides and tutorials they...

Content
Content

Content is king. It's not just about how you're going to write, b...

Events
Events

Looking for an engaging connection with thousands of other people...

Simple Date Accessors - Laravel

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.

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

About author

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.

View all posts (110)
Your experience on this site will be improved by allowing cookies Cookie Policy