reliese / laravel

Reliese Laravel Model Generator
MIT License
1.49k stars 316 forks source link

Remove deprecated $dates cast #254

Closed finiteinfinity closed 1 year ago

finiteinfinity commented 1 year ago

In Laravel 8 the $dates cast was deprecated in favor of using the exitinsg $casts property - in Laravel 10 it is removed entirely. This PR removes the deprecated property and moves date casting to the existing $casts array.

Here is how the change looks:

class User extends Model 
{
    protected $casts = [
        'isAdmin' => 'boolean',
+       'lastLogin' => 'date'
    ];

-    protected $dates = [
-        'lastLogin'
-    ];
}

This approach of handling date casts is supported as far back as Laravel 5.1 so there should be no compatability issues with existing codebases. The only breaking change here is for people who directly accessed the $dates property for whatever reason. If you still need access to only date casts, then the following method can be added to return the same data that would have been held in the property.

public function dates()
{
    return array_keys(array_filter($this->casts, function (string $cast) {
        return $cast === 'date';
    }));
}

Fixes #234