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

wonder95's avatar

Trying to use $casts for date format of YYYY-MM-DD

In my app originally written in Laravel 9, I'm finally updating it to v10. All of my date fields are stored as a MySQL DATE field (i.e. YYYY-MM-DD), and I decided I'd try to use Laravel casting to enforce the format instead of doing it manually. However, after updating my model with the following:

    protected $casts = [
        'birth_date' => 'datetime:Y-m-d'
    ];

    protected $dateFormat = 'Y-m-d';

I now get my birth_date values as a full ISO string when accessed via getter in a resource

'birth_date' => $this->birth_date,

Instead of just the YYYY-MM-DD format. Am I misunderstanding how casting should work for retrieving?

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel, when you use the $casts property with 'datetime:Y-m-d', it is intended to format the date when it is serialized, such as when converting a model to an array or JSON. However, when you access the attribute directly, it will still return a Carbon instance, which is why you're seeing the full ISO string.

To ensure that the birth_date is always formatted as YYYY-MM-DD when accessed, you can define an accessor in your model. Here's how you can do it:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class YourModel extends Model
{
    protected $casts = [
        'birth_date' => 'date', // Use 'date' to cast it to a Carbon instance
    ];

    // Accessor to format the date
    public function getBirthDateAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d');
    }
}

With this accessor, whenever you access the birth_date attribute, it will be formatted as YYYY-MM-DD. The date cast ensures that the attribute is treated as a date, but the accessor customizes the output format.

Remember, the $dateFormat property is used for formatting dates when saving to the database, not when retrieving them. So, it doesn't affect how dates are formatted when accessed.

vincent15000's avatar

I also had the same problem as your one and I think that the date casting is only available to store the date in the database with the right format .

When a column is cast as a date, you may set the corresponding model attribute value to a UNIX timestamp, date string (Y-m-d), date-time string, or a DateTime / Carbon instance. The date's value will be correctly converted and stored in your database.

But if you need to get the date to display it in an input field for example, you need to serialize the date.

You may customize the default serialization format for all of your model's dates by defining a serializeDate method on your model. This method does not affect how your dates are formatted for storage in the database:

/**
 * Prepare a date for array / JSON serialization.
 */
protected function serializeDate(DateTimeInterface $date): string
{
    return $date->format('Y-m-d');
}

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

Can somebody confirm ?

Please or to participate in this conversation.