Laravel Datetime to Carbon Automatically: $dates or $casts

In Laravel, there's a convenient way to transform date/time DB fields to Carbon objects automatically. In fact, there are two ways, let me tell you about both.

Inconvenience: With No Casting

Imagine you have a DB field projects.activated_at with values in the DB like "2022-10-05" or "2021-12-23", so a typical YYYY-MM-DD.

What if you want to show them in another format to users, like "m/d/Y"?

You need to convert them every time, right? And Carbon library would help, something like this:

Carbon::createFromFormat('Y-m-d', $project->activated_at)

So, you need to create a Carbon object yourself, and then you can convert it to whatever you want.

Every time in every place where you want to show that value.

Wouldn't it be cool to have something like this?


So, meet Eloquent $casts and $dates.

$casts and $dates Properties

For long years in Laravel, there has been a property that you can define on Eloquent models, which fields should be auto-transformed into Carbon objects:

class Project extends Model {
protected $dates = [
// ...

And then, everywhere you use this value, in Blade or elsewhere, you get a Carbon object and can use it like in the example above: $project->activated_at->format('m/d/Y')

But a bit later, Laravel introduced a more global concept of Casting, auto-transforming any field to any of the available types, and date/time became just one single example of a broader casting topic.

From then, the officially recommended behavior became to auto-Carbon the dates with $casts property, instead of the older $dates:

class Project extends Model {
protected $casts = [
'activated_at' => 'date',
'deactivated_at' => 'date',

Notice: the older $dates property also still works, at least at the time of writing this article.

Now, the new casting property comes with an additional benefit: you can also provide the format inside the property, like this:

protected $casts = [
'activated_at' => 'date:m/d/Y',
'deactivated_at' => 'date:m/d/Y',

It will not auto-transform the $project->activated_at to the correct format when you access it, but it will do it when you use ->toArray() or ->toJSON() methods on the model.

// Value: Illuminate\Support\Carbon @1664928000 {#5836
// date: 2022-10-05 00:00:00.0 UTC (+00:00),
// }
// Value: 05/10/2022

You can read more about date casting in the official docs.


Great article.

I like to read articles about simple stuff instead of videos, easier to resume or see code whenever needed.

👍 6

Thank you so much for this one. I think I like reading now than watching video haha

Like our articles?

Become a Premium Member for $129/year or $29/month

Recent Premium Tutorials