Laravel:删除ID错误的数据

我使用删除按钮删除数据,但是删除数据时,删除的数据与我所说的行不匹配,但是删除的数据是表顶部的数据


当我使用时return $meja,它应该显示 id 7 但 id 1 相反


欲了解更多详情:


看法 :


@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

这是我的 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>

这是我的控制器:


public function destroy(Meja $meja)

{

    $meja->delete();

    return redirect()->route('meja.index')->with('destroy',' Berhasil dihapus!');

}

这是我的路由器:


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

});


qq_花开花谢_0
浏览 96回答 1
1回答

泛舟湖上清波郎朗

这是因为id冲突。你的每个表单 id 都是,delete但在 dom 中应该有一个带有 id 的元素。当您使用 sweet Alert 提交删除表单时,第一个带有deleteid 的表单已提交,因此第一个项目被删除。使用唯一的 id 或类代替。一个使用类的例子形式<form action="{{ route('meja.destroy',['meja'=>$row->id_meja])}}" class="delete_form" method="POST">&nbsp; &nbsp; @method('DELETE')&nbsp; &nbsp; @csrf</form>甜蜜的警报代码$('.delete_form').submit(function(event) {&nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; Swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Are you sure?',&nbsp; &nbsp; &nbsp; &nbsp; text: "You won't be able to revert this!",&nbsp; &nbsp; &nbsp; &nbsp; icon: 'warning',&nbsp; &nbsp; &nbsp; &nbsp; showCancelButton: true,&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonColor: '#3085d6',&nbsp; &nbsp; &nbsp; &nbsp; cancelButtonColor: '#d33',&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonText: 'Yes, delete it!'&nbsp; &nbsp; }).then((result) => {&nbsp; &nbsp; &nbsp; &nbsp; if (result.isConfirmed) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.currentTarget.submit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript