嗨,我的项目中有简单的问题。我想更新数据库中的数据。 用ajax发送的sku代码。像这样的A-1,A-2,A-3
我尝试过:
$sku2 = $request['sku'];
$sku = explode(',', $sku2);
foreach ($sku as $key => $value){
$content = ProductNew::where('sku', $value)->update(['price' =>
$price]);
}
但是它仅更新了第一条记录(A-1) 问题出在哪里?
这是完全功能
public function setdiscount(Request $request)
{
$discount = Discount::where('id', $request['id'])->first();
$d_price = $discount['price'];
$d_type = $discount['type'];
$price = 17;
$sku2 = $request['sku'];
$sku = explode(',', $sku2);
foreach ($sku as $key => $value){
ProductNew::whereIn('sku', $sku)->update(['price' => $price]);
}
}
答案 0 :(得分:3)
您可以尝试使用where()和update()来更新多个记录:
$sku2 = $request['sku'];
$sku = explode(',', $sku2);
$sku = array_map('trim', $sku);
ProductNew::whereIn('sku', $sku)->update(['price' => $price]);