1

I am sending my data to a controller by Ajax, but on the back-end, I'm getting Array to string conversion.

Controller

<?php

public function updateimages(Request $request){
    $updateDetails=array(
        'image_alt' => $request->get('image_alt')
    );

    $updateIds=array(
        'id' => $request->get('id')
    );

    $images = DB::table('photos')
        ->where([$updateIds])
        ->update([$updateDetails]);

    return response()->json(['updated' => $images], 200);
}

Ajax

<script type="text/javascript">
  $(document).ready(function() {
      $(".SaveImage").on('click', function(e) {
          e.preventDefault();
          e.stopPropagation();

          var idmess= [];
          $(".photohe").each(function(){
            idmess.push($(this).val());
          });

          var image_alt= [];
          $(".image_alt").each(function(){
            image_alt.push($(this).val());
          });

          console.log(idmess);
          console.log(image_alt);
          $.ajax({
              url: '{{ url('admin/savemulti') }}',
              type: 'POST',
              dataType: "JSON",
              data: {
                  "id": idmess,
                  "image_alt": image_alt,
                  "_method": 'POST',
                  "_token": "{{ csrf_token() }}",
              },
              success:function(data) {
                var message = "Image Updated successfully!"
                $('.saved').append(message);
              }
          });
      });
  });
</script>

Here is how my controller dd gets the data:

array:4 [
  "id" => array:2 [
    0 => "1045"
    1 => "1046"
  ]
  "image_alt" => array:2 [
    0 => "mage 1"
    1 => "image 2"
  ]
  "_method" => "POST"
  "_token" => "SParC5rwYy0KLLhwa0km7fcZvodSqrzhvyqZUqk3"
]

It's supposed to update it like:

1045 = mage 1
1046 = image 2

Any idea how to fix it?

2
  • Are you getting array to string conversion error ? Commented Nov 28, 2018 at 2:14
  • @SagarGautam yes Commented Nov 28, 2018 at 2:16

1 Answer 1

1

You can't do multiple updates with the way you are doing in above code. You are adding of ids and data to be updated separately in two arrays.

You have to change your code something like this.

public function updateimages(Request $request){
    $ids = $request->get('id');
    $updateImages = $request->get('image_alt');

    foreach($ids as $key => $value){
        DB::table('photos')
          ->where('id',$value)
          ->update(['image_alt'=>$updateImages[$key]]);
    }

    return DB::table('photos')->whereIn('id',ids)->get();
}
Sign up to request clarification or add additional context in comments.

4 Comments

i'm getting this now SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update photos` set 0 = mage 1 where id = 1045)`
I think it's getting 0 from array 0 => 1045 my console return this as front-end results (2) […] ​ 0: "1045" ​ 1: "1046" ​ (2) […] ​ 0: "mage 1" ​ 1: "image 2"
@mafortis With $key=>$value it should work I think, let me see
@mafortis My bad, while updating we have to give column name and value, i'd missed that. Now try the updated one.

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.