Laravel保存($ request-> all())错误

Arg*_*tya 4 php sql laravel laravel-5 laravel-5.3

我有一个问题,我一直在开发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 ())
Run Code Online (Sandbox Code Playgroud)

这是我的商店功能

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";
}
Run Code Online (Sandbox Code Playgroud)

并且只有在我使用时才会出现错误

$product->save($request->all());

但如果我取消注释评论代码并使用

$product->save()

相反,它工作,没有错误.

任何人都可以帮我找到这个问题的根源吗?

Ale*_*nin 12

由于您没有使用质量分配功能,因此您应该$product->save();在此处使用.

如果您想使用质量分配,请执行以下操作:

public function store(Request $request)
{
    Product::create($request->all());
}
Run Code Online (Sandbox Code Playgroud)