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

murilo's avatar
Level 10

laravel 11 cast date

helo , I dont know why , but seams that in laravel 11 , it is not casting properly the dates . this is my model code -

 protected $guarded  = ['id' , 'created_at' , 'updated_at'];
    protected $hidden = [
        'created_at','updated_at'
    ];

    protected $fillable = [
        'name',
        'date_birth' 
    ];

    protected function casts(): array
    {
        return [
            'date_birth' => 'date:d/m/Y'];
    }


it shold return the data like this -

"date_birth": 01/05/1993

but is returning like this -

"date_birth": "1993-05-01T00:00:00.000000Z",

I tryed like this as well -

   protected $casts = [
        'status' => GenderEnum::class,
        'date_birth' =>  'date:d/m/Y'
    ];
0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It looks like you're trying to cast the date_birth attribute to a specific date format in Laravel 11, but it's not working as expected. The issue might be related to how you're defining the casts in your model.

In Laravel, the casts property should be defined as a public property, not as a method. Also, the date format should be specified correctly. Here’s how you can do it:

  1. Define the $casts property correctly.
  2. Use the date cast type with the desired format.

Here’s the corrected model code:

class YourModel extends Model
{
    protected $guarded  = ['id', 'created_at', 'updated_at'];
    protected $hidden = ['created_at', 'updated_at'];

    protected $fillable = ['name', 'date_birth'];

    protected $casts = [
        'date_birth' => 'date:d/m/Y',
    ];
}

Additionally, if you want to ensure that the date is always formatted correctly when serialized to JSON, you can use an accessor. Here’s how you can add an accessor to your model:

class YourModel extends Model
{
    protected $guarded  = ['id', 'created_at', 'updated_at'];
    protected $hidden = ['created_at', 'updated_at'];

    protected $fillable = ['name', 'date_birth'];

    protected $casts = [
        'date_birth' => 'date',
    ];

    public function getDateBirthAttribute($value)
    {
        return \Carbon\Carbon::parse($value)->format('d/m/Y');
    }
}

In this example, the getDateBirthAttribute method ensures that the date_birth attribute is always formatted as d/m/Y when accessed.

With these changes, your date_birth attribute should be returned in the desired format:

"date_birth": "01/05/1993"

Make sure to replace YourModel with the actual name of your model. This should resolve the issue you’re facing with date casting in Laravel 11.

Snapey's avatar

The correct answer is to follow this guidance. Unfortunately only present in the Laravel 7 upgrade notes

https://laravel.com/docs/7.x/upgrade#date-serialization

Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models. To format dates for serialization, the framework now uses Carbon's toJSON method, which produces an ISO-8601 compatible date including timezone information and fractional seconds. In addition, this change provides better support and integration with client-side date parsing libraries.

Previously, dates would be serialized to a format like the following: 2019-12-02 20:01:00. Dates serialized using the new format will appear like: 2019-12-02T20:01:00.283041Z. Please note that ISO-8601 dates are always expressed in UTC.

If you would like to keep using the previous behavior you can override the serializeDate method on your model:

Please or to participate in this conversation.