Use Rector to automatically refactor legacy protected $casts array properties into the modern casts() method across your entire Laravel codebase.
Laravel 11 introduced the casts() method on Eloquent models, allowing fluent cast definitions, class references, and method calls inside model classes.
Rector automates converting legacy protected $casts properties to the new method:
1) app/Models/Post.php
- protected $casts = [
- 'tags' => 'array',
- 'published_at' => 'datetime',
- 'is_featured' => FeaturedStatus::class,
- ];
+ protected function casts(): array
+ {
+ return [
+ 'tags' => 'array',
+ 'published_at' => 'datetime',
+ 'is_featured' => FeaturedStatus::class,
+ ];
+ }
- Runs across hundreds of models in seconds during framework upgrades
- Enables calling static methods directly inside cast definitions (e.g.
AsEnumCollection::of(...)) - Eliminates typos in array property names
Related Tips
View all tips →Refresh and Pessimistically Lock Models with refreshForUpdate() in Laravel 13.27
Laravel 13.27 adds refreshForUpdate(), reloading an existing model instance in-place with fresh database attributes while acquiring an exclusive FOR UPDATE row lock.
Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.