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

    <tfoot id='Xhweo'></tfoot>
    1. <small id='Xhweo'></small><noframes id='Xhweo'>

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

      1. 什么是“批量分配"?在 Laravel 中是什么意思

        时间:2023-09-24
      2. <tfoot id='l8Yq0'></tfoot>

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

          • <small id='l8Yq0'></small><noframes id='l8Yq0'>

                <legend id='l8Yq0'><style id='l8Yq0'><dir id='l8Yq0'><q id='l8Yq0'></q></dir></style></legend>

                    <tbody id='l8Yq0'></tbody>
                  本文介绍了什么是“批量分配"?在 Laravel 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我浏览有关 Eloquent ORM 主题部分的 Laravel 文档时,我得到了一个新术语批量分配".

                  When I went through Laravel Document about Eloquent ORM topic part, I got a new term "Mass Assignment".

                  文档显示如何进行批量分配和 $fillable$guarded 属性设置.但是经历了那之后,我对批量分配"并没有清楚的了解.以及它是如何工作的.

                  Document show How to do Mass Assignment and the $fillable or $guarded properties settings. But after went through that, I didn't have a clearly understand about "Mass Assignment" and how it works.

                  在我过去使用 CodeIgniter 的经验中,我也没有听说过这个术语.

                  In my past experience in CodeIgniter, I also didn't hear about this term.

                  有人对此有一个简单的解释吗?

                  Does anyone have a simple explanation about that?

                  推荐答案

                  Mass assignment 是当你发送一个数组到模型创建时,基本上是一次性在模型上设置一堆字段,而不是一个一个,类似:

                  Mass assignment is when you send an array to the model creation, basically setting a bunch of fields on the model in a single go, rather than one by one, something like:

                  $user = new User(request()->all());
                  

                  (这不是分别在模型上显式设置每个值.)

                  (This is instead of explicitly setting each value on the model separately.)

                  您可以使用 fillable 来保护您希望它实际允许更新的字段.

                  You can use fillable to protect which fields you want this to actually allow for updating.

                  您还可以通过执行以下操作阻止所有字段可批量分配:

                  You can also block all fields from being mass-assignable by doing this:

                  protected $guarded = ['*'];
                  

                  假设在您的用户表中有一个字段是 user_type 并且可以具有 user/admin 的值

                  Let's say in your user table you have a field that is user_type and that can have values of user / admin

                  显然,您不希望用户能够更新此值.理论上,如果您使用上述代码,有人可以将 user_type 的新字段注入表单并将admin"与其他表单数据一起发送,然后轻松地将他们的帐户切换到管理员帐户... 坏消息.

                  Obviously, you don't want users to be able to update this value. In theory, if you used the above code, someone could inject into a form a new field for user_type and send 'admin' along with the other form data, and easily switch their account to an admin account... bad news.

                  通过添加:

                  $fillable = ['name', 'password', 'email'];
                  

                  您确保只有那些值可以使用 mass assignment

                  You are ensuring that only those values can be updated using mass assignment

                  为了能够更新 user_type 值,您需要在模型上显式设置并保存它,如下所示:

                  To be able to update the user_type value, you need to explicitly set it on the model and save it, like this:

                  $user->user_type = 'admin';
                  $user->save();
                  

                  这篇关于什么是“批量分配"?在 Laravel 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 急切加载限制 下一篇:Laravel Eloquent Sum of关系的列

                  相关文章

                  最新文章

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

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

                    • <bdo id='eSDpT'></bdo><ul id='eSDpT'></ul>
                      <tfoot id='eSDpT'></tfoot>

                    1. <legend id='eSDpT'><style id='eSDpT'><dir id='eSDpT'><q id='eSDpT'></q></dir></style></legend>