0

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..

11
  • can you post your error? and $role->pluck('id') return array because whereIn wnat an array as 2 parameters. ex (->whereIn('id', [1, 2, 3])) Oct 19, 2017 at 9:22
  • @LorenzoBerti I do update with using ajax.. so, the error only displayed in inspect element
    – Jems
    Oct 19, 2017 at 9:24
  • foreach($updateArray as $array){ DB::table('role_memberships')->where('role_id', $id)->whereIn('role_id', $role->pluck('id'))->update($array); } Oct 19, 2017 at 9:25
  • @SamuelJames I have used the way like you give, but the problem is my data have the same value.. example: id = 1, role_id = 1, I do $request->PostAdMaxImage = 'hello' id = 2, role_id = 1, I do $request->PostAdExpiredDay = 'world'. then, I save and re-open again.. my value of PostAdMaxImage and PostAdExpiredDay turn into 'world', in the other word, it only take the last value
    – Jems
    Oct 19, 2017 at 9:30
  • 1
    @abr oh i see.. thankyou for the feedback ;)
    – Jems
    Oct 19, 2017 at 10:07

7 Answers 7

5

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);
5

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); 
}
0
2

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 ;)

0
1

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

1
  • when I do return DB::table('role_memberships')->where('role_id', $id)->whereIn('role_id', $role->pluck('id'))->get(); It return membership data with id = 1 and id = 2
    – Jems
    Oct 19, 2017 at 9:40
0

Try this one. it works to me .

  DB::table('role_memberships')->where('role_id', $id)-
>update(['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay]);
2
  • this does not work for me.. I have tried the way like this.. and the result still same, it take the last record.. But I don't understand, the response in inspect element show like this [{"id":2,"RoleName":"zxdas","IsAllCategory":false,"IsUserCanLogin":false,"created_at":"2017-10-19 14:55:46","updated_at":"2017-10-19 14:55:46"},[{"role_id":"2","MembershipName":"PostAdMaxImage","MembershipValue":"hello"},{"role_id":"2","MembershipName":"PostAdExpiredDay","MembershipValue":"world"}]]
    – Jems
    Oct 19, 2017 at 9:39
  • I think you have a problem in checking the role id, do you check this variable "$id"? try to remove the "whereIn('role_id', $role->pluck('id'))" in query check agan the query .
    – Kenneth
    Oct 19, 2017 at 9:46
0

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];
0

I suggest using the upsert() method given from Laravel eloquent model, its a multiple update or create method, you can just simply pass the needed data in an array with the ids of each column and specify the unique column last set the columns that needs to be updated and you will obtain the result you are looking for.

for more information read the docs: https://laravel.com/docs/9.x/eloquent#upserts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.