Laravel/Eloquent 建议覆盖特征 属性?

Laravel/Eloquent advises to override a trait property?

我在 Laravel 5.4 中使用 Eloquent 个模型 在文档中,我看到:

You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}

但是,$fillable 属性 已经在所有模型使用的特征中定义:

trait GuardsAttributes
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

...

PHP 文档清楚说明了 Traits 属性:

If a trait defines a property then a class can not define a property with the same name, otherwise an error is issued. It is an E_STRICT if the class definition is compatible (same visibility and initial value) or fatal error otherwise.

Laravel 文档中有关建议实施的错误吗?

您不能像 PHP 文档所建议的那样在 class 中覆盖特征属性。

然而,

Laravel 要求您在 child class(您的模型 class 扩展了 Eloquent 模型 class 并且特征包含在 Eloquent 模型 class 中,而不是您的模型 class)。这是一件完全正确的事情!