Skip to content
Advertisement

Laravel: Delete data with wrong ID

I use the Delete button to delete data, but when deleting data, the deleted data does not match the rows I mean, but the data that is deleted is the data that is at the top of the table

when I use return $meja, it should appear id 7 but id 1 instead

For further details :

view :

@foreach($data as $row)
    <tr>
        <th scope="row">{{$no++}}</th>
        <td>{{$row->no_meja}}</td>
        <td>{{$row->keterangan}}</td>
        <td>
            <a href="{{route('meja.edit',['meja'=>$row->id_meja])}}" class="btn btn-success"><i class="fas fa-edit"></i></a>
            <a href="#" data-id="" class="btn btn-danger confirm_script mr-3">
                <form action="{{ route('meja.destroy',['meja'=>$row->id_meja])}}" id="delete" method="POST">
                    @method('DELETE')
                    @csrf
                </form>
                <i class="fas fa-trash"></i>
            </a>
        </td>
    </tr>
@endforeach

here is my javascript

<script>
    $(".confirm_script").click(function(e) {
        // id = e.target.dataset.id;
        swal({
            title: 'Yakin hapus data?',
            text: 'Data yang dihapus tidak bisa dibalikin',
            icon: 'warning',
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                $('#delete').submit();
            } else {
                swal('Your imaginary file is safe!');
            }
        });
    });
</script>

Here is my controller :

public function destroy(Meja $meja)
{
    $meja->delete();
    return redirect()->route('meja.index')->with('destroy',' Berhasil dihapus!');
}

Here is my Router :

Route::group(['prefix' => 'admin'], function() {
    Route::view('/','admin/dashboard.index');
    //---MASAKAN---//
    Route::resource('masakan','MasakanController');
 Route::post('admin/masakan/updatestatus/{masakan}','MasakanController@UpdateStatus')->name('masakan.updateStatus');
    //---MEJA---///
    Route::resource('meja','MejaController');
    //---ADMIN-ACCOUNT---/
    Route::resource('adminaccount','AdminController');   
});

Advertisement

Answer

it is because of id confliction. your every form id is delete but in a dom there should be a single element with an id. when you are using sweet alert to submit the delete form, the very first form with delete id got submitted, hence the first item got deleted. use unique id or class instead. an example for you using class

form

<form action="{{ route('meja.destroy',['meja'=>$row->id_meja])}}" class="delete_form" method="POST">
    @method('DELETE')
    @csrf
</form>

sweet alert code

$('.delete_form').submit(function(event) {
    event.preventDefault();
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            event.currentTarget.submit();
        }
    })
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement