0

How can i update multiple rows use laravel eloquent?

I want something like:

$guest = GuestJointDetail::where('guest_joint_id', $guest_joint_id)
    ->where('sort_no', '>', $sort_no)
    ->whereNull('deleted_at');

$guest->update(['sort_no' => DB::raw('sort_no - 1')]);

I have tried

$guest->update(['sort_no' => 10]);

and i worked!
So i think my problem is in DB::raw

Thank you so much!

1
  • @DhruvRaval: yes, that's why i don't know how Commented Aug 29, 2018 at 4:01

2 Answers 2

2

use decrement();

guest = GuestJointDetail::where('guest_joint_id', $guest_joint_id)
    ->where('sort_no', '>', $sort_no)
    ->whereNull('deleted_at')
    ->decrement('sort_no',1);

use whereIn for multiple Ids:

guest = GuestJointDetail::whereIn('guest_joint_id', $guest_id_array)
        ->where('sort_no', '>', $sort_no)
        ->whereNull('deleted_at')
        ->decrement('sort_no',1);
Sign up to request clarification or add additional context in comments.

1 Comment

That's what i want, tks so much!
1

You can use decrement():

$guest = GuestJointDetail::where('guest_joint_id', $guest_joint_id)
    ->where('sort_no', '>', $sort_no)
    ->whereNull('deleted_at')
    ->decrement('sort_no');

4 Comments

'decrement' clause is what i want, but like @dhruv's answer, it must be decrement('sort_no', 1). Anw, tks so much
decrement('sort_no') is enough, the default $amount is 1.
oh yes, that's true, sorry for my mistake
yes 1 is default value. but for your understanding i pass 1. you can change 2nd value as per your requirement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.