当我更新表的某些列时,它将在表中插入新字段
"id": 1,
"invoice_id": "0001-2019-20",
"client_id": "1",
"currency_id": "4",
"date": "2019-06-06",
"subject": "wordpress",
"total": "3000"
当我仅更新主题和总计时,它将插入新字段
"id": 2,
"invoice_id": "",
"client_id": "0",
"currency_id": "0",
"date": "0000-00-00",
"subject": "OMS",
"total": "0"
这是我的控制器,
$invoices = new Invoice;
$invoices->client_id = $request->get('client_id');
$invoices->currency_id = $request->get('currency_id');;
$invoices->subject = $request->get('subject');
$invoices->save();
答案 0 :(得分:0)
您正在使用的代码用于创建新记录。 但是你快到了。
因此,假设您要更新以下数据
{"id": 1,
"invoice_id": "0001-2019-20",
"client_id": "1",
"currency_id": "4",
"date": "2019-06-06",
"subject": "wordpress",
"total": "3000"}
使用以下代码更新该行。
$invoices = Invoice::find($request->get('id'));
$invoices->client_id = $request->get('client_id');
$invoices->currency_id = $request->get('currency_id');
$invoices->subject = $request->get('subject');
$invoices->save();
还有以下替代方法。
$invoices = Invoice::find($request->get('id'));
$invoices->update([
'client_id' => $request->get('client_id'),
'currency_id' => $request->get('currency_id'),
'subject' => $request->get('subject')
]);
注意:对于上述方法数组键名和表列名 必须相同。