Laravel 的 model 的 fillable 和 guarded 屬性

                                                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方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章