我正在插入一个包含多个输入的数组。但是当我添加或创建它时,它不会插入或返回任何值。在这种情况下如何使用 create ?我是 Laravel 的新手。foreach ($data['sku'] as $key => $val) { $attrCountSKU = ProductsAttribute::where('sku', $val)->count(); if ($attrCountSKU > 0) { return back()->with('error', 'SKU already exists for this product! Please input another SKU.'); } $attrCountSizes = ProductsAttribute::where(['product_id' => $product->id, 'size' => $data['size'][$key]])->count(); if ($attrCountSizes > 0) { return back()->with('error', 'Size already exists for this product! Please input another Size.'); } $attribute = new ProductsAttribute; $attribute->product_id = $product->id; $attribute->sku = $val; $attribute->size = $data['size'][$key]; $attribute->price = $data['price'][$key]; $attribute->stock = $data['stock'][$key]; dd($attribute); dd($attribute->create());}
2 回答
呼如林
TA贡献1798条经验 获得超3个赞
而不是$attribute->create()你应该使用$attribute->save()方法。
或者使用create()您可以这样做的方法
$flight = ProductsAttribute::create(
[
'product_id' => $product->id,
'sku' => $val,
'size' => $data['size'][$key],
'price' => $data['price'][$key],
'stock' => $data['stock'][$key],
]
);
- 2 回答
- 0 关注
- 137 浏览
添加回答
举报
0/150
提交
取消