Delete a record from table using ajax in laravel 5

后端 未结 4 863
故里飘歌
故里飘歌 2021-01-13 06:30

I want to delete the record using ajax.

view

@foreach( $products as $product )
    
        {{ $product->code         


        
4条回答
  •  伪装坚强ぢ
    2021-01-13 07:17

    Your problem is here:

    var dataId = $('#btnDeleteProduct').attr('data-id');
    

    You are only going to get the first one, because you are using a duplicate id on all buttons.. So an id sizzle query will get you the first one.

    You will see this if you dd($id) in your delete controller. It's always the id of the first. You can get the data-id attribute by querying relative to event.target.

    You will want to use the debugger; statement inside your call back to test this, but the query should be something like:

    $(e.target).attr('data-id');
    

    or

    $(this).attr('data-id');
    

    The event target should be the button that was clicked, and you set the data-id on that button, so you just need to query it via the event.

提交回复
热议问题