Ad

Laravel Forge Deployment Fail With Php 7.4 Typed Property On User Model

I create fresh new project in Laravel 7 with php 7.4 and on User model I have this properties:

* The attributes that are mass assignable.
     *
     * @var array
     */
    protected array $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected array $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected array $casts = [
        'email_verified_at' => 'datetime',
    ];

and on Laravel Homestead everything works fine but when I want to deploy app with Laravel Forge on UserSeeder I got this message:

PHP Fatal error:  Type of App\User::$casts must not be defined (as in class Illuminate\Foundation\Auth\User)
Ad

Answer

The reason you're getting this error is because the $casts property defined in the parent class isn't typed as an array property but it is in your model.

According to the PHP RFC: Typed Properties 2.0:

Property types are invariant. This means that the type of a (non-private) property is not allowed to change during inheritance (this includes adding or removing property types).


To get around this issue I would suggest simply removing array from your inherited properties.

I would also suggest reading this article for more information on typed properties and other PHP 7.4 changes.

Ad
source: stackoverflow.com
Ad