我在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文档建议的同一个类中覆盖特征属性.
但是,Laravel要求您在子类中覆盖它(您的模型类扩展了Eloquent模型类,并且特征包含在Eloquent模型类中,而不是您的模型类中).这是一个非常有效的事情!