Laravel Forge 部署失败,在用户模型上输入 php 7.4 属性

Laravel Forge Deployment fail with php 7.4 typed property on User model

我在 Laravel 7 中用 php 7.4 创建了新项目,在用户模型上我有这个属性:

* 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',
    ];

在 Laravel Homestead 上一切正常,但是当我想在 UserSeeder 上使用 Laravel Forge 部署应用程序时,我收到了这条消息:

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

您收到此错误的原因是因为父项 class 中定义的 $casts 属性 未键入为 array 属性 但它在您的模型中。

根据 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).


为了解决这个问题,我建议简单地从您的继承属性中删除 array

我还建议阅读 this article 以获取有关类型化属性和其他 PHP 7.4 更改的更多信息。