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

What does quot;Mass Assignmentquot; mean in Laravel?(什么是“批量分配?在 Laravel 中是什么意思?)
本文介绍了什么是“批量分配"?在 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 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)