Laravel 的 model 的 fillable 和 guarded 屬性
所有的Eloquent模型預設會防止批量賦值,所以需要在Model中設置fillable和guarded屬性。
一、fillable爲白名單,表示該字段可被批量賦值;guarded爲黑名單,表示該字段不可被批量賦值。
protected $fillable = ['name'];
protected $guarded = ['password'];
二、可爲所有屬性設置黑名單:
protected $guarded = ['*'];
三、laravel的create方法爲批量賦值,save方法爲逐個手動賦值,因此fillable和guarded對save方法不起作用而用於create方法。