douqiang5809 2018-05-06 18:01
浏览 26
已采纳

Laravel获取集合删除任何内容

I am sending collection of id's by checkbox to my controller to delete them all together, I get network 200 but nothing will delete.

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');

any idea why is that?

UPDATE

if i change my ajax data to serialize() instead of val() i get

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

and when i delete then only id = 35 will delete, `only one of them instead of all of them.

  • 写回答

2条回答 默认 最新

  • duanhuang2150 2018-05-06 18:15
    关注

    You are getting an array of IDs. So this would work.

    $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();
    }
    

    Or you could do all this in Laravel observables and in here just write

    Product::whereIn('id', $request->productsfordel)->delete();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭