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

vincent15000's avatar

Date casting in a model

Hello,

I have a little problem with the date casting in a model.

protected $casts = [
    'payment_date' => 'date:Y-m-d',
];

The casting helps to format the property when I get it, for example with $book->payment_date.

I have specified the format Y-m-d, but when I display the value in the view, it show 2023-02-19 00:00:00.

The problem is that I only want the date and not the time. Effectively if I want to display this datetime in a date type input, it doesn't work.

I probably do something wrong, but I don't see what.

Or perhaps in that case it is necessary to write an accessor ?

Can you help me ?

Thanks a lot.

Vincent

0 likes
9 replies
Sinnbeck's avatar

How are you using it? Query + usage

1 like
vincent15000's avatar

@Sinnbeck Yes I retrieve the model from the database Model::find($id) and then I display the date in the view.

{{ $book->payment_date }}
Lara_Love's avatar

Hello @vincent15000 use carbon and like

Carbon::createFromFormat('d/m/Y', $request->stockupdate)->format('Y-m-d')
1 like
vincent15000's avatar

What is strange is that when I check the date with dd(), it show me only the date and not the time.

dd($book); // I check the date and it's ok
dd($book->payment_date); // I check the date and it's date and time
vincent15000's avatar

@LoverToHelp I'm looking for a way to not have to format the property inside the blade view.

That's why I'm trying to format it via the casting which is a pretty good solution to get the dates as a Carbon object.

But ... oh as I explain this I wonder if this is not the solution. A Carbon object can't be only a date, it's necessarily a date and time object. I will try something.

vincent15000's avatar

@LoverToHelp In fact what I'm trying to do is to have the date as a Carbon object but only the date formatted as a string with the format method.

Perhaps it's simply not a good idea to cast the date.

Lara_Love's avatar

Dear @vincent15000 Unfortunately, I have two problems

1- I am not a professional

4- My English is not good, I don't know exactly what is being talked about to comment more

1 like

Please or to participate in this conversation.