<tfoot id='gH1ZT'></tfoot>

    <legend id='gH1ZT'><style id='gH1ZT'><dir id='gH1ZT'><q id='gH1ZT'></q></dir></style></legend>
      • <bdo id='gH1ZT'></bdo><ul id='gH1ZT'></ul>

        <small id='gH1ZT'></small><noframes id='gH1ZT'>

        <i id='gH1ZT'><tr id='gH1ZT'><dt id='gH1ZT'><q id='gH1ZT'><span id='gH1ZT'><b id='gH1ZT'><form id='gH1ZT'><ins id='gH1ZT'></ins><ul id='gH1ZT'></ul><sub id='gH1ZT'></sub></form><legend id='gH1ZT'></legend><bdo id='gH1ZT'><pre id='gH1ZT'><center id='gH1ZT'></center></pre></bdo></b><th id='gH1ZT'></th></span></q></dt></tr></i><div id='gH1ZT'><tfoot id='gH1ZT'></tfoot><dl id='gH1ZT'><fieldset id='gH1ZT'></fieldset></dl></div>

        模型中的 Laravel 动态可填充

        时间:2023-09-23

        <tfoot id='P6oxy'></tfoot>
        • <bdo id='P6oxy'></bdo><ul id='P6oxy'></ul>
          <legend id='P6oxy'><style id='P6oxy'><dir id='P6oxy'><q id='P6oxy'></q></dir></style></legend>
              <tbody id='P6oxy'></tbody>

                <i id='P6oxy'><tr id='P6oxy'><dt id='P6oxy'><q id='P6oxy'><span id='P6oxy'><b id='P6oxy'><form id='P6oxy'><ins id='P6oxy'></ins><ul id='P6oxy'></ul><sub id='P6oxy'></sub></form><legend id='P6oxy'></legend><bdo id='P6oxy'><pre id='P6oxy'><center id='P6oxy'></center></pre></bdo></b><th id='P6oxy'></th></span></q></dt></tr></i><div id='P6oxy'><tfoot id='P6oxy'></tfoot><dl id='P6oxy'><fieldset id='P6oxy'></fieldset></dl></div>
              • <small id='P6oxy'></small><noframes id='P6oxy'>

                  本文介绍了模型中的 Laravel 动态可填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  遇到了 Laravel 5.2 的问题.

                  以下是eloquent create操作(post call)时的错误,

                  Model.php 453 中的批量赋值异常:column_name

                  以下是需要考虑的先决条件:

                  1. 模型中的可填充物由以下代码以动态方式填充:<前>公共函数 __construct() {$this->fillable(Schema::getColumnListing($this->getTable()))}

                  目前调试的方法如下:

                  1. 在插入之前,在控制器中,$model::getillableField() 给出了适当的可填充数组.

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

                    <前>如果 ($this->isFillable($key)) {$this->setAttribute($key, $value);}

                    上面的代码返回值为false",$model::getFillableField() 在数组列表中有 column_name.

                  3. 使用表列对 $fillable 变量进行硬编码可消除错误.请帮助,我哪里出错了,解决方法是什么?

                  提前致谢.

                  解决方案

                  您真正想做的是使所有字段可填写.

                  在 Laravel 中执行此操作的正确方法是:

                  protected $guarded = [];

                  这在 5.2 中有效,即使它的文档在 5.3 中找到.

                  (相关源码对于 5.2)

                  (来自 5.3 的文档):

                  <块引用>

                  如果你想让所有的属性都可以批量赋值,你可以将 $guarded 属性定义为一个空数组:

                  通过将 $guarded 设置为一个空数组,您正在创建一个空的黑名单,允许所有字段都可以批量分配.

                  此外,如果此模型永远将直接根据用户输入构建,请不要这样做.Laravel 要求定义 $fillable$guarded 是有原因的.除非您的模型具有与公共表单 1:1 的字段,否则允许所有字段在批量分配时可写是一个安全漏洞.

                  Got stuck in a issue with laravel 5.2.

                  Following is the error during eloquent create operation(post call),

                  Mass Assignment Exception in 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:

                    public function __construct() {
                         $this->fillable(Schema::getColumnListing($this->getTable()))
                    }
                    

                  Following are the methods which are debugged till now:

                  1. Before insertion, in controller, $model::getillableField(), gives proper fillable array.

                  2. In model.php line(450),

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

                    the above code returns the value as "false" and $model::getFillableField() has the column_name in the array list.

                  3. Hardcoding $fillable variable with columns of table removes the error. 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:

                  protected $guarded = [];
                  

                  This works in 5.2, even though the documentation for it is found in 5.3.

                  (relevant source code for 5.2)

                  (Documentation from 5.3):

                  If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array:

                  By setting $guarded to an empty array, you are creating an empty black list, allowing all fields to be mass assignable.

                  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. 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.

                  这篇关于模型中的 Laravel 动态可填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 变形关系 下一篇:如何将 Laravel Query Builder 结果作为整数获取

                  相关文章

                  最新文章

                  <tfoot id='MkdNJ'></tfoot>
                  <legend id='MkdNJ'><style id='MkdNJ'><dir id='MkdNJ'><q id='MkdNJ'></q></dir></style></legend>
                • <i id='MkdNJ'><tr id='MkdNJ'><dt id='MkdNJ'><q id='MkdNJ'><span id='MkdNJ'><b id='MkdNJ'><form id='MkdNJ'><ins id='MkdNJ'></ins><ul id='MkdNJ'></ul><sub id='MkdNJ'></sub></form><legend id='MkdNJ'></legend><bdo id='MkdNJ'><pre id='MkdNJ'><center id='MkdNJ'></center></pre></bdo></b><th id='MkdNJ'></th></span></q></dt></tr></i><div id='MkdNJ'><tfoot id='MkdNJ'></tfoot><dl id='MkdNJ'><fieldset id='MkdNJ'></fieldset></dl></div>

                  1. <small id='MkdNJ'></small><noframes id='MkdNJ'>

                        <bdo id='MkdNJ'></bdo><ul id='MkdNJ'></ul>