title: How to store data in laravel 8 using Ajax Call with multiple images
body: <p>I am trying to store data in a database in Laravel, but the issue is I am able to save the directly using controller.</p>
  <p>Now I am using image order change that it working with the jQuery.</p>
  <p>After changing orders of the images, I get the json data in which images sources are stored.</p>
  <p>Now I want to store the data with image file location(that I will get when I use laravel file storage to convert base64 image src and save it in the disk).</p>
  <p>So, only the image src are coming from the josn, the rest I am using <code>Laravel request-&gt;input()</code> for.</p>
  <p>Here is the Ajax code:</p>
  <pre><code>$(document).ready(function() { 
      // Initialize sortable
      $(&quot;#sortable&quot;).sortable();
  
      // Save order
      $('#submit').click(function() {
          var imageids_arr = [];
          // get image ids order
          $('#sortable li').each(function() {
              var id = $(this).data('id');
              var json = {
                             id : id,
                             src : $(this).data('src')
                         }
              imageids_arr.push(json);
          });
  
          console.log(imageids_arr);
          var ajaxurl = '/api/add_ref_images_first';
  
          // AJAX request
          $.ajax({
                      url: ajaxurl,
                      type: 'get',
                      data: {imageids: imageids_arr},
                      success: function(response){
                          if(response == 1)
                              alert('Save successfully.');
                      }
                  });
              });
  });
  </code></pre>
  <p>Controller code:</p>
  <pre><code> public function add_product(Request $request) {
      $rules = array(
          'name' =&gt; 'required',
          'author' =&gt; 'required',
          'short_description' =&gt; 'required',
          'description' =&gt; 'required',
          'parent_id' =&gt; 'required',
          'tags' =&gt; 'required',
          'price' =&gt; 'required',
          'sku' =&gt; 'required',
          'publisher' =&gt; 'required',
          'thumbnail' =&gt; 'required',
          'product_images' =&gt; 'required',
      );
      $messages = [
  
      ];
  
      $validator = Validator::make($request-&gt;all(),$rules, $messages);
  
      if ($validator-&gt;fails()) {
          return redirect()-&gt;back()-&gt;withErrors($validator)-&gt;withInput();
      } else {
          $product_id = 'PUBP' . time();
          $name = $request-&gt;input('name');
          $author = $request-&gt;input('author');
          $short_description = $request-&gt;input('short_description');
          $long_description = $request-&gt;input('description');
          $parent_id = $request-&gt;parent_id;
          $tags = $request-&gt;input('tags');
          $price = $request-&gt;input('price');
          $sku = $request-&gt;input('sku');
          $quantity = $request-&gt;input('quantity');
          $tax = $request-&gt;input('tax');
          $selling_price = $request-&gt;input('offer_price');
          $added_by = Cookie::get('admin_id');
          $publisher = $request-&gt;input('publisher');
          $thumbnail = &quot;&quot;;
          $thumbnail = $this-&gt;thumbnail($request-&gt;file('thumbnail'), $product_id);
          $product_images = &quot;&quot;;
          $product_images = $this-&gt;product_images($request-&gt;file('product_images'), $product_id);
  //            Note :  Here I want the save the image file and get the location
  //            return $parent_id;
          // Saving Data
          $result = Product::create([
              'product_id' =&gt; $product_id,
              'name'=&gt;$name,
              'author'=&gt;$author,
              'slug' =&gt; \Str::slug($name),
              'short_description'=&gt;$short_description,
              'description' =&gt; $long_description,
              'parent_id' =&gt; $parent_id,
              'tags' =&gt; $tags,
              'price' =&gt; $price,
              'sku' =&gt; $sku,
              'publisher' =&gt; $publisher,
              'thumbnail' =&gt; $thumbnail,
              'images' =&gt; $product_images,
              'added_by' =&gt; $added_by,
              'quantity' =&gt; $quantity,
              'tax' =&gt; $tax,
              'offer_price' =&gt; $selling_price,
              'meta_title' =&gt; $name,
              'meta_description'=&gt; $short_description,
              'meta_keywords'=&gt; $tags,
              'status' =&gt; '0',
          ])-&gt;categories()-&gt;sync(explode(',',$parent_id));
  
          if($result){
              return redirect('/admin/products')-&gt;with( [ 'status' =&gt; &quot;Product Uploaded Successfully&quot; ] );
          }else{
              return redirect('/admin/products')-&gt;with( [ 'status' =&gt; &quot;Something went wrong&quot; ] );
          }
  
      }
  
  }
  </code></pre>
  <p>Ref image store code:</p>
  <pre><code>public function product_images($ref_images, $folder_name) {
      $fileName = &quot;&quot;;
      $end_url = &quot;&quot;;
      $count = 0;
  
      foreach($ref_images as $image)
      {
          $ref_image_id = 'PUBR'.time().$count++.'.'.$image-&gt;getClientOriginalExtension();
          $fileName = $image-&gt;storeAs($folder_name, $ref_image_id , ['disk' =&gt; 'my_uploaded_files']);
          if($fileName){
              $end_url = $end_url.$fileName.',';
          }
      }
      return $end_url;
  }
  </code></pre>
  <p>Json data screenshot:</p>
  <p><a href="https://i.stack.imgur.com/IHjX8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IHjX8.png" alt="enter image description here" /></a></p>