【问题标题】:Laravel Protecting Columns from Mass Assignment that Aren't Nullable/Default ValuesLaravel 保护列免受不可为空/默认值的批量分配
【发布时间】:2019-03-05 06:50:03
【问题描述】:

我正在尝试在我的用户模型中保护“姓名”和“电子邮件”属性,因为我不希望我的用户在注册后能够更改它们。

我的用户模型如下所示:

protected $fillable = [
    'province',
    'city',
    'street',
    'postal',
    'cellphone',
    'facebook',
    'instagram',

];

protected $guarded = [
  'name',
  'email',
  'password',
  'account_type',
  'verified_type',
];

注册后,Laravel 默认批量分配这些值,如下所示:

//Create the user
      $user = User::create([
          'name' => $data['name'],
          'email' => $data['email'],
          'password' => Hash::make($data['password']),
          'province' => $data['province'],
          'city'  => $data['city'],
          'street'  => $data['street'],
          'postal'  => $data['postal'],
          'cellphone' => $data['cellphone'],
          'trial_ends_at' => \Carbon\Carbon::now()->addMonths(3),
          'account_type' => $accountType,
      ]);

但这对我来说会引发错误,因为“名称”没有默认值并且不能为空。我明白为什么我会收到错误以及如何解决它,但我想知道如果他们没有默认/可为空的属性,我应该如何分配名称和电子邮件。例如,类似:

$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->save();

$user->update([
  //the rest of the mass assignable values
]);

或者有没有更简单的方法?

【问题讨论】:

  • 来自docs(查找批量分配部分)。 “当然,你应该使用 $fillable$guarded - 不能同时使用。”
  • 啊,谢谢。我可以很容易地删除一个——但这清楚地表明了我想要解决的问题:)
  • 您只需要在注册时接受“姓名”和“电子邮件”,而不是在更新时接受?
  • 正确,我不希望他们能够改变自己的身份
  • @Vranvs 为什么不在更新时将它们从请求中删除?

标签: php mysql database laravel security


【解决方案1】:

您可以通过将其添加到您的模型来完成。

/*
    Attribute which are protected from updating.
*/

protected $protected = [
    'name', 'email'
];

protected static function boot()
{
    parent::boot();

    static::saving(function ($model) {
        if($model->id){
            foreach($model->protected as $attribute){
                $model->$attribute = $model->getOriginal($attribute);
            }
        }
    });
}

希望代码是自我表达的。

【讨论】:

    【解决方案2】:

    您可以使用 mutator 并从受保护的属性中删除名称。 read the docs here

    public function setNameAttribute($newName)
    {
      if(isset($this->name) && $this->name !== null){
        throw new \Exception;
        //or do nothing
      } else {
        $this->attributes['name'] = $newName;
      }
    }
    

    对电子邮件也这样做

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2023-03-25
      • 2015-06-24
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多