简体   繁体   English

Laravel动态可填充模型

[英]Laravel Dynamic Fillable in Models

Got stuck in a issue with laravel 5.2. 在laravel 5.2中遇到了问题。

Following is the error during eloquent create operation(post call), 以下是eloquent创建操作(后调用)期间的错误,

Mass Assignment Exception in Model.php 453: column_name Model.php 453中的批量分配异常:column_name

Following are the prerequisites, which are to be taken into consideration: 以下是要考虑的先决条件:

  1. Fillables in model are filled in a dynamic manner by the following code: 通过以下代码以动态方式填充模型中的可填充内容:
    \npublic function __construct() { public function __construct(){\n     $this->fillable(\\Schema::getColumnListing($this->getTable())) $这个 - >可填写(\\架构:: getColumnListing($这个 - > getTable()))\n} }\n

Following are the methods which are debugged till now: 以下是到目前为止调试的方法:

  1. Before insertion, in controller, $model::getillableField(), gives proper fillable array. 在插入之前,在控制器中,$ model :: getillableField()给出了适当的可填充数组。

  2. In model.php line(450), 在model.php行(450)中,

    \nif ($this->isFillable($key)) { if($ this-> isFillable($ key)){\n      $this->setAttribute($key, $value); $ this-> setAttribute($ key,$ value);\n} }\n

    the above code returns the value as "false" and $model::getFillableField() has the column_name in the array list. 上面的代码返回值为“false”,$ model :: getFillableField()在数组列表中有column_name。

  3. Hardcoding $fillable variable with columns of table removes the error. 用表格列硬编码$ fillable变量会删除错误。 Please Help, where i am going wrong and what is the solution for it? 请帮忙,我哪里出错了,解决方案是什么?

Thanks in advance. 提前致谢。

What you are really trying to do is make ALL fields fillable. 你真正想做的是让所有字段都可填写。

The correct way to do this in Laravel is this: 在Laravel中执行此操作的正确方法是:

protected $guarded = [];

This works in 5.2, even though the documentation for it is found in 5.3. 这适用于5.2,即使它的文档在5.3中找到。

( relevant source code for 5.2 ) 5.2的相关源代码

( Documentation from 5.3 ): 5.3的文件 ):

If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array: 如果您想使所有属性都可以分配,您可以将$ guarded属性定义为空数组:

By setting $guarded to an empty array, you are creating an empty black list, allowing all fields to be mass assignable. 通过将$guarded设置$guarded空数组,您将创建一个空的黑名单,允许所有字段都是可批量分配的。

Also, if this model is ever going to be constructed directly from user input, please do not do this. 另外,如果这种模式是以往任何时候都直接从用户输入构造,请不要这样做。 Laravel requires either $fillable or $guarded to be defined for a reason. Laravel要求要么$fillable$guarded的一个原因来定义。 Unless your model has fields that are literally 1:1 with a public form, then allowing all fields to be writable on mass assignment is a security vulnerability. 除非您的模型具有字面上1:1的公共表单字段,否则允许所有字段在批量分配上可写是一个安全漏洞。

Try this. 尝试这个。 Put the below code in your model, 将以下代码放入您的模型中,

public function __construct()
{
    $this->setFillable();
}
public function setFillable()
{
    $fields = \Schema::getColumnListing('table_name_here');

    $this->fillable[] = $fields;
}

This makes each and every column is fillable from that table. 这使得每个列都可以从该表中填充。

Create a trait that uses the database columns. 创建使用数据库列的特征。

<?php

namespace App\Traits;

use Illuminate\Support\Facades\Schema;

trait ColumnFillable
{
    public function getFillable()
    {
        return Schema::getColumnListing($this->getTable());
    }
}

Now use this trait in your models. 现在在模型中使用此特征。

<?php

namespace App;

use App\Traits\ColumnFillable;

class MyModel extends Model
{
    use ColumnFillable;

    ...

Now you'll never have to manually specify $fillable . 现在您永远不必手动指定$fillable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM