Reputation: 1706
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..
Upvotes: 0
Views: 18437
Reputation: 93
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
Upvotes: 1
Reputation: 51
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);
}
Upvotes: 5
Reputation: 497
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);
Upvotes: 5
Reputation: 1706
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 ;)
Upvotes: 2
Reputation: 2129
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];
Upvotes: 0
Reputation: 2993
Try this one. it works to me .
DB::table('role_memberships')->where('role_id', $id)-
>update(['MembershipValue' => $request->PostAdMaxImage, 'MembershipValue' => $request->PostAdExpiredDay]);
Upvotes: 0
Reputation: 6974
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
Upvotes: 1