How to update multiple row with different ID in Laravel

This is from the last question that I asked..

I have a membership table

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

I have two rows data with same role_id and I want to update two rows
This is my controller
$updateArray = [['MembershipValue' => $request->PostAdMaxImage], 
['MembershipValue' => $request->PostAdExpiredDay]];
DB::table('role_memberships')->where('role_id', $id)-
>whereIn('role_id', $role->pluck('id'))->update($updateArray);

when I tried to update, I got an error array to string conversion..

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

UPDATE BATCH (BULK) IN 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);

Method 2

It works for me, even if the thread is 2 years ago I would like to share my solution too, I hope this can help somebody.

In blade

<input name="quantity[]">
<input name="total[]">
<input name="prodId[]">

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

Method 3

I think this is only the way that I can used..

DB::table('role_memberships')->where('role_id', $id)->where('MembershipName','PostAdMaxImage')->update(['MembershipValue' => $request->PostAdMaxImage]);
DB::table('role_memberships')->where('role_id', $id)->where('MembershipName','PostAdExpiredDay')->update(['MembershipValue' => $request->PostAdExpiredDay]);

If you guys has the best way or the shortest way, please let me know.. thankyou 😉

Method 4

There are 2 error:

1) The whereIn function want an array for parameter as

$users = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();

2) The update function want an simple array for parameter like
DB::table('users')
            ->where('id', 1)
            ->update(['votes' => 1]);

You ar passing an [[], []]

So you have to pass example $model->update(['MembershipValue' => $request->PostAdMaxImage])

here you found the complete documentation for update and whereIn https://laravel.com/docs/5.5/queries

Method 5

Try this one. it works to me .

  DB::table('role_memberships')->where('role_id', $id)-
>update(['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay]);

Method 6

I have two rows data with same role_id and I want to update two rows This is my controller

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

when I tried to update, I got an error array to string conversion..

That is because you have an array within an array on your $updateArray. Fixing it like this should correct your error:

$updateArray = ['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay];


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x