检索 Laravel 中的可填写字段
Retrieve fillable fields in Laravel
在 laravel 5.4 中,我可以使用模型实例的 fillable
索引检索可填写字段。
$model = new AnyClass();
dd($model['fillable']);
以上代码打印 AnyClass
的所有可填写字段。但是相同的代码在 laravel 5.6 上打印 null
。我知道我可以使用 $model->getFillable()
检索可填写的字段。我的问题是它在 laravel 5.6 中不起作用但在 5.4 中起作用的原因是什么?
来自升级指南here我相信这就是问题的答案:
Model Methods & Attribute Names
To prevent accessing a model's private properties when using array access, it's no longer possible to have a model method with the same name as an attribute or property. Doing so will cause exceptions to be thrown when accessing the model's attributes via array access ($user['name']
) or the data_get
helper function.
如果您查看 Laravel 的源代码,您会发现不同之处。
模型 class 由应用程序模型扩展,实现了 ArrayAccess
接口,其中强制 class 定义 offsetGet
方法。
在 Laravel 5.4 中,offsetGet
方法如下所示:
public function offsetGet($offset)
{
return $this->$offset;
}
这意味着如果你调用 $model['fillable']
,你实际上调用了 $model->offsetGet('fillable')
,实际上 returns fillable
属性 class .
我找不到 Laravel 5.6 标签,但我很确定它与 Laravel 5.5.45 的代码相同。在此版本中,offsetGet
方法更改为:
public function offsetGet($offset)
{
return $this->getAttribute($offset);
}
这意味着它实际上 returns 如果找到该属性,否则为空。
将 class 中的 属性 改为 public $fillable = [
而不是 protected $fillable = [
在 Laravel 7 中,我通过在模型的新实例上调用 getFillable 方法来完成此操作。像这样:
$model = new MyModel();
$fillable = $model->getFillable();
派对迟到了,但我不喜欢必须始终实例化模型的概念,特别是如果您使用 Eloquent serialization。
假设您想要构建一些过滤器,但想要根据模型的可填充项将列列入白名单。您不想实例化整个模型,因此您可以改用反射:
(new ReflectionClass(MyModel::class))->getDefaultProperties()['fillable']
See it working over at 3v4l.org - 在这里,我将演示为什么您可能不想实例化此模型,因为它具有序列化并且总是急切加载。
在 laravel 5.4 中,我可以使用模型实例的 fillable
索引检索可填写字段。
$model = new AnyClass();
dd($model['fillable']);
以上代码打印 AnyClass
的所有可填写字段。但是相同的代码在 laravel 5.6 上打印 null
。我知道我可以使用 $model->getFillable()
检索可填写的字段。我的问题是它在 laravel 5.6 中不起作用但在 5.4 中起作用的原因是什么?
来自升级指南here我相信这就是问题的答案:
Model Methods & Attribute Names
To prevent accessing a model's private properties when using array access, it's no longer possible to have a model method with the same name as an attribute or property. Doing so will cause exceptions to be thrown when accessing the model's attributes via array access (
$user['name']
) or thedata_get
helper function.
如果您查看 Laravel 的源代码,您会发现不同之处。
模型 class 由应用程序模型扩展,实现了 ArrayAccess
接口,其中强制 class 定义 offsetGet
方法。
在 Laravel 5.4 中,offsetGet
方法如下所示:
public function offsetGet($offset)
{
return $this->$offset;
}
这意味着如果你调用 $model['fillable']
,你实际上调用了 $model->offsetGet('fillable')
,实际上 returns fillable
属性 class .
我找不到 Laravel 5.6 标签,但我很确定它与 Laravel 5.5.45 的代码相同。在此版本中,offsetGet
方法更改为:
public function offsetGet($offset)
{
return $this->getAttribute($offset);
}
这意味着它实际上 returns 如果找到该属性,否则为空。
将 class 中的 属性 改为 public $fillable = [
而不是 protected $fillable = [
在 Laravel 7 中,我通过在模型的新实例上调用 getFillable 方法来完成此操作。像这样:
$model = new MyModel();
$fillable = $model->getFillable();
派对迟到了,但我不喜欢必须始终实例化模型的概念,特别是如果您使用 Eloquent serialization。
假设您想要构建一些过滤器,但想要根据模型的可填充项将列列入白名单。您不想实例化整个模型,因此您可以改用反射:
(new ReflectionClass(MyModel::class))->getDefaultProperties()['fillable']
See it working over at 3v4l.org - 在这里,我将演示为什么您可能不想实例化此模型,因为它具有序列化并且总是急切加载。