批量赋值
批量赋值主要用于快速设置模型属性
新增或修改
Eloquent模型时, 依次为每个属性赋值, 数量多时操作麻烦$matchReply = new MatchReply;$matchReply->join_id = $matchJoin->id;$matchReply->user_id = $user['id'];$matchReply->question_id = $priorQuestion->id;$matchReply->title = $priorQuestion->title;
批量赋值允许我们以数组的方式将待设置属性以关联数组的方式传递构造函数
// 效果等价于上面的代码 $matchReply = new MatchReply([ 'join_id' => $matchJoin->id, 'user_id' => $user['id'], 'question_id' => $priorQuestion->id, 'title' => $priorQuestion->title, ]);批量赋值的黑白名单
```php /**
- 使用批量赋值的属性(白名单) *
- @var array */ protected $fillable = [];
/**
- 不使用批量赋值的字段(黑名单) *
- @var array / protected $guarded = [‘‘]; ```
- Eloquent 模型类默认白名单属性为空,黑名单属性为
*,即所有字段都不会应用批量赋值 - 对于频繁变动的数据表,建议使用白名单,这样安全性更好,因为哪些字段应用批量赋值始终是可控的,黑名单则会在后续新增字段的时候容易遗漏
- 对于相对稳定或者字段很多的数据表,建议使用黑名单,免去设置字段之苦
