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 →Reusable Migration Fields with Custom Blueprint Macros
Extend Laravel's Blueprint class with custom macros to standardize and reuse common schema columns across database migrations.
Define Custom Macros on the Eloquent Builder
Register reusable query methods directly on the Builder so every model gains access without repeating logic.