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

Hala's avatar
Level 3

Convert created_at to date type

Hello , i try to cast created_at to be date i added this in the model

protected $casts = [
    'created_at' => 'date'
];

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

but did not work it's return back as datetime

0 likes
3 replies
MichalOravec's avatar

When you show data

$model->created_at->toDateString();

When you make a query

->whereDate('created_at', '2020-08-26');

Or just in your migration use

$table->date('created_at');
Hala's avatar
Level 3

@michaloravec thank you , but i want to save it as datetime and return it with api as date

Please or to participate in this conversation.