0

I want update an array of ids.

enter image description here

Eloquent Query

$my_ids = json_encode($request->post('my_ids'));
Lead::whereIn('id', $my_ids)->update(['op' => 2]); 

but I need to pass a string with comma.

enter image description here

How can I solve it?

7
  • $my_ids is an array from strings. If you want update all of them you should use foreach: foreach($my_ids as $id){Lead::whereIn('id', $id)->update(['op' => 2]); } Commented Apr 17, 2022 at 10:02
  • In this way not runs a lot of query? I'd like to make a query only. Commented Apr 17, 2022 at 10:04
  • In your solution, convert $my_ids to int.like this:(int)$my_ids Commented Apr 17, 2022 at 10:11
  • tried, no error but doesn't work. Commented Apr 17, 2022 at 10:17
  • 2
    Your variable is string. Use json_decode Commented Apr 17, 2022 at 10:26

1 Answer 1

2

I believe the ids you receive from post request is object string so you need to decode that string in order to get an array.

$my_ids = json_decode($request->post('my_ids'), true);
Lead::whereIn('id', $my_ids)->update(['op' => 2]); 
Sign up to request clarification or add additional context in comments.

Comments

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.