模型 Laravel 中的可填写字段太多?
Too many fillable fields in model Laravel?
我在 table 中有大约 200 个编号的字段:
field_1
field_2
etc
我试图在 table 中插入数据:
Result::insert($data);
其中 $data
是多数组:
$data = [] = array("field_1" => 3);
$data = [] = array("field_1" => 2);
我可以在选项 protected $fillable = ["*"];
中设置 *
以使所有字段都可填写吗?
在这种情况下,您可以尝试相反的操作。例如:id
、created_at
和 updated_at
字段作为 $guarded。喜欢:
protected $guarded = ['id', 'created_at', 'updated_at'];
除此之外其余部分将被视为 fillable
,即 可大量分配。
您可以在 Official Laravel Doc
中找到详细信息
Guarding Attributes
While $fillable serves as a "white list" of attributes that should be
mass assignable, you may also choose to use $guarded. The $guarded
property should contain an array of attributes that you do not want to
be mass assignable. All other attributes not in the array will be mass
assignable. So, $guarded functions like a "black list". Of course,
you should use either $fillable or $guarded - not both.
如果您需要将所有列设置为可填充,请在模型中执行此操作:
protected $guarded = [];
If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array
我在 table 中有大约 200 个编号的字段:
field_1
field_2
etc
我试图在 table 中插入数据:
Result::insert($data);
其中 $data
是多数组:
$data = [] = array("field_1" => 3);
$data = [] = array("field_1" => 2);
我可以在选项 protected $fillable = ["*"];
中设置 *
以使所有字段都可填写吗?
在这种情况下,您可以尝试相反的操作。例如:id
、created_at
和 updated_at
字段作为 $guarded。喜欢:
protected $guarded = ['id', 'created_at', 'updated_at'];
除此之外其余部分将被视为 fillable
,即 可大量分配。
您可以在 Official Laravel Doc
中找到详细信息Guarding Attributes
While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a "black list". Of course, you should use either $fillable or $guarded - not both.
如果您需要将所有列设置为可填充,请在模型中执行此操作:
protected $guarded = [];
If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array