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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
mafortis
  • 6,750
  • 23
  • 130
  • 288

1 Answers1

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();
}
Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • 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)` – mafortis Nov 28 '18 at 02:27
  • 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 Nov 28 '18 at 02:30
  • @mafortis With $key=>$value it should work I think, let me see – Sagar Gautam Nov 28 '18 at 02:32
  • @mafortis My bad, while updating we have to give column name and value, i'd missed that. Now try the updated one. – Sagar Gautam Nov 28 '18 at 02:34