比较来自世界各地的卖家的域名和 IT 服务价格

请求更新B. laravel 5.4

如何更新多行

我落在肿块以下


[
"3",
"4",
"5"
]


我的控制器


$answers= $request->get/'option'/; //answers array
foreach /$answers as $answer/
{
Answer::where/'question_id', 1/->update/
['customer_id' => 1,
'answer' => $answer]/;
}
已邀请:

莫问

赞同来自:

如果使用查询构建器,则使用下面的查询。


DB::table/'answers'/
->where/'question_id', 2/
->update/array/'customer_id' => 1, 'answer' => 2]/;


如果你使用 Eloquent ORM, 然后使用以下请求


App\Answers::where/'question_id', 2/->update/['customer_id' => 1,'answer'=>2]/;

君笑尘

赞同来自:

您可以使用简单的下一个查询更新它。


DB::table/'answers'/->where/'id',2/->update/['customer_id' => 1, 'answer' => 2]/;


最好使用模型 Eloquent, 例如,


Answer::where/'id',2/->update/['customer_id' => 1, 'answer' => 2]/;


如果您未将此列添加到属性中
$fillable

模型,然后你可以用它来更新 after find like,


$answer = Answer::find/2/;
$answer->customer_id = 1;
$answer->answer = 2;
$answer->save//;


我希望你明白。

小明明

赞同来自:

您可以使用该方法
update//

:


Answer::where/'question_id', 2/->update/['customer_id' => 1, 'answer' => 2]/;


方法 update 期望一系列的一对列和值表示必须更新的列

不要忘记添加
customer_id

,
answer

在阵列中
https://laravel.com/docs/5.4/e ... nment
:


protected $fillable = ['customer_id', 'answer'];

要回复问题请先登录注册