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

fri*_*nux 5 php traits laravel eloquent laravel-5.4

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

您还可以使用create方法将新模型保存在一行中.插入的模型实例将从方法返回给您.但是,在执行此操作之前,您需要在模型上指定可填充或保护属性,因为默认情况下所有Eloquent模型都会防止批量分配.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];
}
Run Code Online (Sandbox Code Playgroud)

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

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

...
Run Code Online (Sandbox Code Playgroud)

PHP文档清楚了解Traits属性:

如果特征定义了属性,则类无法定义具有相同名称的属性,否则将发出错误.如果类定义兼容(相同的可见性和初始值)或其他致命错误,则为E_STRICT.

Laravel文档是否对于所提供的实现是错误的?

Par*_*ras 7

您不能在PHP文档建议的同一个类中覆盖特征属性.

但是,Laravel要求您在子类中覆盖它(您的模型类扩展了Eloquent模型类,并且特征包含在Eloquent模型类中,而不是您的模型类中).这是一个非常有效的事情!