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

Stengi's avatar

Date casting is not functioning as expected

Hello dear Laracasters 👋🏻,

I am currently facing a very annoying problem regarding casting my dates and datetime values.

My users table has a birthday column of the type date.

However, when I try to cast the type to date, I get a datetime format back.

Let's say the stored birthday is 1985-03-15, with the following code I would expect my results to be something like that:

My Code (User Model):

protected $casts = [
    'birthday' => 'date:d.m.Y',
];

Expected result: 15.03.1985

Actual result: 1985-03-15 00:00:00

My question is: Why?

Thank you very much in advance! 👋🏻

0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

The date cast format is used whenever the Model instance is being serialized, not for general use.

When defining a date or datetime cast, you may also specify the date's format. This format will be used when the model is serialized to an array or JSON:

https://laravel.com/docs/8.x/eloquent-mutators#date-casting

You can use an accessor to format the date for Blade:

public function getBirthdayFormattedAttribute()
{
	return $this->birthday->format('d.m.Y');
}

Which you can use as $user->birthday_formatted on Model instances

1 like
Stengi's avatar

Hi, thank you very much for the fast reply.

I was thinking about using an accessor at first but it seems kind of odd to me. Why can I specify the date/datetime format in my casts but it's not being used?

Or am I missing the point here?

tykus's avatar

It is only for serialized models (i.e. JSON representation of the model)

Stengi's avatar

Ahh, yeah you said that in the post prior.

Guess I didn't pay enough attention while reading it.

Thank you very much. 😊

Stengi's avatar

It is giving me the same result. 😟

tykus's avatar

Did you understand my reply; try my suggestion using an Accessor method.

Stengi's avatar

I did 😄

It just took me a while to reply.

Please or to participate in this conversation.