Courses

Laravel 11 Eloquent: Expert Level

Model casts(): Dates, Enum and More

Summary of this lesson:
- Implementing attribute casting in models
- Working with datetime casts and Carbon
- Setting up Enum casts
- Understanding differences between Laravel 10 and 11 casting syntax

In your Eloquent Models, you can provide casts to automatically cast database columns to some type that you would need to use separately with that type of logic. Probably the most popular example is about date type.


In the User Model, by default, the email_verified_at is cast to datetime.

class User extends Authenticatable
{
// ...
 
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}

What does this cast mean? It means that it will be automatically...

The full lesson is only for Premium Members.
Want to access all 28 lessons of this course? (71 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord