2

I have ids in a string through ajax,

example:

"1,2"

and in my controller, I would like to update multiple rows of a product in one query using whereIn with id => [1,2].

ajax:

$('#add_product').click(function() { 
   var id = $('#get_value').val();
    $.ajax({
        type: 'POST',
        url: "{{action('ProductController@updateProduct')}}",
        data: {
        id: id
        },
     success: function(data){
          alert("success");
     }
    });
});

Controller :

public function updateProduct(Request $request){
 $test_value = collect($request->id);
 $updateProduct = Product::whereIn('id',$test_value)
                  ->update(['title' => 'My title']);
}

But i have this error in the network :

SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: '1,2' (SQL: update "products" set "title" = "My title" where "id" in (1,2))

4
  • just dd($test_value) show the output Commented Apr 10, 2018 at 11:20
  • dd($test_value) show's me => "1,2" Commented Apr 10, 2018 at 11:32
  • @sebastianlarsen have you overcome this problem with my answer? Commented Apr 10, 2018 at 12:47
  • @ChiragPatel, yes thank's it works very well (y) Commented Apr 10, 2018 at 13:36

3 Answers 3

4

In $test_value you are getting values "1,2" as a string so you have to make it array first with using explode() function(what explode() function do is here),

$ids = explode(',',$test_value);

then pass this $ids into your function.

public function updateProduct(Request $request){
    $test_value = $request->id;
    $ids = explode(',',$test_value);
    $updateProduct = Product::whereIn('id',$ids)
                  ->update(['title' => 'My title']);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to remove collect()

public function updateProduct(Request $request){
   $test_value = $request->id;
   $ids = explode( ',',$test_value);
   $updateProduct = Product::whereIn('id',$ids)
              ->update(['title' => 'My title']);
}

Comments

-1

You can not update the fields in the table - bulk. You need to update one by one through the foreach

public function updateProduct(Request $request){
 $updateProducts = [];

 foreach((array) $request->id as $id) {
  $updateProducts[] = Product::where('id', $id)
                                      ->first()
                                      ->update(['title' => 'My title']);
 }
}

3 Comments

Thank's Mike Foxtech , but he make me an update for id = 1 but for id = 2 he does not do anything
@sebastianlarsen $request->id looks like an array, that's right [1, 2] ?
You CAN bulk update the fields in a table. In fact, if you are not specific enough; your query will affect all the table.

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.