Laravel获取集合删除任何内容

时间:2018-05-06 18:01:19

标签: php ajax laravel

我正在向我的控制器发送ID by checkbox的集合,将它们全部删除,我收到网络200但没有任何内容会删除

javascript

<script type="text/javascript">
  $(document).ready(function() {
    $('#multidel').on('click', function(e) {
      e.preventDefault();
      $.ajaxSetup({
          headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
      });

      $.ajax({
        url: '{{ url('admin/delmultipleproducts') }}',
        type: "POST",
        dataType: "json",
        data: $('.productsfordel:checked').serialize(),
        success:function(data) {
          console.log('done');
        }
      });

      $(document).ajaxStop(function(){
          alert('successfully deleted.');
          window.location.reload();
      });

    });
  });
</script>

controller

public function multipledel(Request $request){

        $product = Product::where('id', $request->productsfordel);
        if(!empty($product->suboptions)){
          $product->suboptions()->detach();
        }
        if(!empty($product->subspecifications)){
          $product->subspecifications()->detach();
        }
        if(!empty($product->relatives)){
          $product->relatives()->detach();
        }
        if(!empty($product->imageOne)){
          Storage::delete($product->imageOne);
        }
        if(!empty($product->imageTwo)){
          Storage::delete($product->imageTwo);
        }
        $product->delete();

      // Session::flash('success', 'Selected products are successfully deleted.');
      // return redirect()->route('products.index');
    }

route

  Route::post('delmultipleproducts', 'ProductController@multipledel')->name('delmultipleproducts');

任何想法为什么会这样?

更新

如果我将我的ajax数据更改为serialize()而不是val()

array:3 [
  0 => "35"
  1 => "34"
  2 => "33"
]

当我删除时,只有id = 35会删除,`只删除其中一个而不是全部。

2 个答案:

答案 0 :(得分:2)

我想你会在这里归还这个系列 -

$product = Product::where('id', $request->productsfordel);

您应该实际返回第一个产品

$product = Product::where('id', $request->productsfordel)->get()->first();

或者,如果您确定它已在主键上编入索引(即您的ID是主键

你应该做

$product = Product::find($request->productsfordel);

或者如果你愿意的话 根据要求删除所有产品

foreach($request->productsfordel as $id) {
 $product = Product::find($id);
        if(!empty($product->suboptions)){
          $product->suboptions()->detach();
        }
        if(!empty($product->subspecifications)){
          $product->subspecifications()->detach();
        }
        if(!empty($product->relatives)){
          $product->relatives()->detach();
        }
        if(!empty($product->imageOne)){
          Storage::delete($product->imageOne);
        }
        if(!empty($product->imageTwo)){
          Storage::delete($product->imageTwo);
        }
        $product->delete();

答案 1 :(得分:1)

您正在获取一系列ID。所以这会奏效。

$products = Product::whereIn('id', $request->productsfordel)->get();
foreach($products as $product){
  $product->suboptions()->detach();
  $product->subspecifications()->detach();
  $product->relatives()->detach();
  if(!empty($product->imageOne)){
      Storage::delete($product->imageOne);
    }
  if(!empty($product->imageTwo)){
    Storage::delete($product->imageTwo);
  }
  $product->delete();
}

或者你可以在Laravel observables中完成所有这些,并在这里写一下

Product::whereIn('id', $request->productsfordel)->delete();