Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

podu-file-plus's avatar

Remove time from activation date

In my subscription model I have a date called activated_at. When I display this date it shows like 2019-09-24 00:00:00. I only want to display the date without calling "format" everywhere. How can I do that?

My model

protected $dates = ['activated_at', 'created_at', 'updated_at'];
0 likes
2 replies
bobbybouwmann's avatar
Level 88

You can create an accessor for this field. This way you can do the format part just once and all the other dates stay the same.

//Model

public function getActivatedAtAttribute($value)
{
    return $value->format('Y-m-d');
}

Documentation: https://laravel.com/docs/6.x/eloquent-mutators#accessors-and-mutators

Note: You will now get a string back instead of the Carbon object, so keep that in mind

Please or to participate in this conversation.