title: Laravel sending multiple id to controller by ajax
body: <p>I want to make multiple delete in laravel 5.5, I have tried forms it didn't work so i decided to use ajax instead.</p>
  
  <h3>What I try to do:</h3>
  
  <ol>
  <li>Use checkbox to select multiple post/product id's.</li>
  <li>Send them to controller by ajax.</li>
  <li>Delete them.</li>
  </ol>
  
  <h2>Codes</h2>
  
  <p><code>controller</code></p>
  
  <pre><code>public function multipledel(Request $request){
        $deli = $request-&gt;input('productsfordel'); //get id's of selected post/products
        $product = Product::where('id', [$deli]); //find those id's in DB
        $product-&gt;delete(); // Delete them
  
        Session::flash('success', 'Selected products are successfully deleted.');
        return redirect()-&gt;route('products.index');
      }
  </code></pre>
  
  <p><code>route</code></p>
  
  <pre><code>Route::post('delmultipleproducts', 'ProductController@multipledel')-&gt;name('delmultipleproducts');
  </code></pre>
  
  <p><code>ajax</code></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(document).ready(function() {
      $('#multidel').on('click', function(e) {  //define which button to click for multiple delete
        e.preventDefault();
        $.ajaxSetup({
            headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
        });
  
        var idss = $('#productsfordel').val(); //where to get id's from
        if(idss) {
        $.ajax({
          url: '{{ url('admin/delmultipleproducts') }}',
          type: "POST",
          dataType: "json",
          success:function(data) {
            console.log('working');
          }
        });
        }else{
          console.log('not working');
        } 
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><code>blade</code></p>
  
  <pre><code>&lt;button id="multidel" name="multidel"&gt;Delete All&lt;/button&gt;
  
  &lt;thead&gt;
    &lt;th class="text-center"&gt;xxxxx&lt;/th&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    @foreach($products as $product)
    &lt;tr&gt;
      &lt;td&gt;
        &lt;input type="checkbox" id="productsfordel" name="productsfordel[]" value="{{ $product-&gt;id }}" /&gt;
      &lt;/td&gt;
  
  // rest of table
  </code></pre>
  
  <h3>Issues</h3>
  
  <p>this is what i get in my network as result:</p>
  
  <p><a href="https://i.stack.imgur.com/lTYDl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lTYDl.png" alt="screen 1"></a></p>
  
  <p>Any idea?</p>
  
  <h1>UPDATE</h1>
  
  <p>Base on <strong>Babak</strong> answer I can get some result on network which means id's are actually sending, here is error:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`mysite`.`product_relatives`, CONSTRAINT `product_relatives_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)) (SQL: delete from `products` where `id` = 48)
  </code></pre>
  
  <p>so I realize it must have to do something with my products sync method such as options and specification and i changed my function to:</p>
  
  <pre><code>public function multipledel(Request $request){
        $deli = $request-&gt;input('productsfordel');
        $product = Product::where('id', [$deli]);
  // added from here
        $product-&gt;suboptions()-&gt;detach();
        $product-&gt;subspecifications()-&gt;detach();
        if(!empty($product-&gt;imageOne)){
          Storage::delete($product-&gt;imageOne);
        }
        if(!empty($product-&gt;imageTwo)){
          Storage::delete($product-&gt;imageTwo);
        }
  //to here
        $product-&gt;delete();
  
        Session::flash('success', 'Selected products are successfully deleted.');
        return redirect()-&gt;route('products.index');
      }
  </code></pre>
  
  <p>now i'm getting:</p>
  
  <pre><code>Call to undefined method Illuminate\Database\Query\Builder::suboptions()
  </code></pre>
  
  <p>any idea?</p>