Laravel save($request->all()) 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43115364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel save($request->all()) Error
提问by Arga Aditya
I have a question, I've been developing laravel application and I encountered this strange error:
我有一个问题,我一直在开发 Laravel 应用程序,但遇到了这个奇怪的错误:
QueryException in Connection.php line 770:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`testenv`.`products`, CONSTRAINT `products_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`)) (SQL: insert into `products` () values ())
and Here's my store function
这是我的商店功能
public function store(Request $request)
{
$product = new Product;
/*
$product->name = $request->name;
$product->stockQty = $request->stockQty;
$product->minimumQty = $request->minimumQty;
$product->description = $request->description;
$product->notes = $request->notes;
$product->tenantMargin = $request->tenantMargin;
$product->length = $request->length;
$product->height = $request->height;
$product->weight = $request->weight;
$product->showLength = $request->showLength;
$product->showHeight = $request->showHeight;
$product->showWeight = $request->showWeight;
$product->size = $request->size;
$product->colorName = $request->colorName;
$product->colorHex = $request->colorHex;
$product->isActive =$request->isActive;
$product->tenant_id = $request->tenant_id;
$product->productviewid = $request->productviewid;
*/
$product->save($request->all());
return "product successfully created";
}
And the error only occurs if i use
并且只有在我使用时才会发生错误
$product->save($request->all());
$product->save($request->all());
but if I un-comment the the commented code and use
但是如果我取消注释注释的代码并使用
$product->save()
$product->save()
instead, it works, no error.
相反,它有效,没有错误。
Can anyone help me to find the source of this problem?
谁能帮我找到这个问题的根源?
回答by Alexey Mezenin
Since you're not using the mass assignmentfeature, you should use $product->save();
here.
由于您没有使用批量分配功能,您应该$product->save();
在这里使用。
If you want to use the mass assignment, just do this:
如果要使用质量分配,只需执行以下操作:
public function store(Request $request)
{
Product::create($request->all());
}
回答by GiamPy
As Alexey said, $product->save()
it's going to use the data sent in the function instead of whatever you've set before, just like what you are trying to do in your example.
正如 Alexey 所说,$product->save()
它将使用函数中发送的数据,而不是您之前设置的任何数据,就像您在示例中尝试做的那样。
That way you are notusing the mass assignment feature, because you are specifying every single field that needs to be updated in the database.
这样您就不会使用批量分配功能,因为您正在指定需要在数据库中更新的每个字段。
$product->save($parameters)
actually makes use of the mass assignment feature and those attributes need to fillable, or un-guarded in the laravel properties.
$product->save($parameters)
实际上利用了质量分配功能,并且这些属性需要在 Laravel 属性中填充或不受保护。
Note that also Model::create()
supports mass assignments.
请注意,它还Model::create()
支持批量分配。