假设我们有一个 Topic 提交 html 表单,它使用以下输入字段名称:
name
text
在 Controller 级别,我们可能已经写了这样的东西:
public function create(Request $request) {
// Validation logic ...
Topic::create($request->all());
}
如果客户端用户添加
id
会怎样?输入字段:id
name
text
Laravel 也会填充
id
新模型对象的字段?我可以守护
id
模型,或者我应该使用 $request->only()
?
最佳答案
您不需要使用 $fillable
数组或 $guarded
主键是 id
默认情况下。当您使用 create()
时,Eloquent 不会填充主键方法。
关于php - Laravel - 模型 ID 保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862192/