如何在 Laravel 中更新具有不同 ID 的多行

Jam*_*ady 0 php laravel

这是我问的最后一个问题..

我有一张会员表

Schema::create('role_memberships', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id');
        $table->string('MembershipName');
        $table->text('MembershipValue');
        $table->timestamps();
    });
Run Code Online (Sandbox Code Playgroud)

我有两行具有相同 role_id 的数据,我想更新两行这是我的控制器

$updateArray = [['MembershipValue' => $request->PostAdMaxImage], 
['MembershipValue' => $request->PostAdExpiredDay]];
DB::table('role_memberships')->where('role_id', $id)-
>whereIn('role_id', $role->pluck('id'))->update($updateArray);
Run Code Online (Sandbox Code Playgroud)

当我尝试更新时,出现数组到字符串转换错误。

Moh*_*mad 5

在 Laravel 中批量更新

https://github.com/mavinoo/updateBatch

$table = 'users';

$value = [
      [
          'id' => 1,
          'status' => 'active'
      ],
      [
          'id' => 5,
          'status' => 'deactive',
          'nickname' => 'Ghanbari'
     ],
     [
          'id' => 10,
          'status' => 'active',
         'date' => Carbon::now()
     ],
     [
         'id' => 11,
         'username' => 'mavinoo'
     ]
];

$index = 'id';

UpdateBatch::updateBatch($table, $value, $index);
Run Code Online (Sandbox Code Playgroud)


小智 5

它对我有用,即使该线程是两年前的,我也想分享我的解决方案,我希望这可以帮助别人。

刀片内

<input name="quantity[]">
<input name="total[]">
<input name="prodId[]"> 
Run Code Online (Sandbox Code Playgroud)

控制器

foreach ($request->prodId as $key => $value) {
      $data = array(                 
          'quantity'=>$request->quantity[$key],
          'total'=>$request->total[$key],                   
      );         
      Cart::where('id',$request->prodId[$key])
      ->update($data); 
}
Run Code Online (Sandbox Code Playgroud)