在Laravel中使用Bootstrap Modal窗口确认删除数据

问题描述

我想使用模式删除数据,我已经获得了要删除的数据的ID,但是当我单击模式中的删除按钮时,什么都没有发生。请告诉我是否有问题。非常感谢。

我在获取数据以传输到我的删除确认模式时遇到问题。我已经验证了删除路线可以从数据库删除数据,但是我面临的问题是我无法将categoty->id传递到模式中以进行删除

刀片和ajax

@extends('Admin.master')

@section('content')
    <div class="col-md-10 p-5 pt-2">
        <h3><i class="fas fa-tachometer-alt ml-2"></i>دسته بندی ها</h3><hr>
        <a href="{{ route('categories.create') }}" class="btn btn-primary mb-3">
            <i class="fas fa-plus-square"></i>
            افزودن دسته بندی جدید
        </a>
        <div class="table-responsive">
            <table class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>نام دسته</th>
                    <th>تاریخ ایجاد شده</th>
                    <th>اکشن ها</th>
                </tr>
                </thead>
                <tbody>
                @foreach($categories as $category)
                    <tr>
                        <td>{{ $category->id }}</td>
                        <td>{{ $category->name }}</td>
                        <td>{{ $category->created_at }}</td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ route('categories.edit',$category->id) }}"  class="btn btn-primary">ویرایش</a>
                                <button type="button" class="btn btn-danger" data-category="{{ $category->id }}" data-toggle="modal" data-target="#deleteCategory">حذف</button>
                            </div>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <div class="modal fade" id="deleteCategory" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="deleteCategory" aria-hidden="true">
        <div class="modal-dialog modal-sm" role="document">
            <form action="{{ route('categories.destroy',$category->id) }}">
                @csrf
                @method('DELETE')
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">این عمل برگشت پذیر نیست..</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        آیا از حذف {{ $categories[0]->name }} اطمینان دارید؟
                        <input type="hidden" id="category" name="category_id">
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn bg-white" data-dismiss="modal">بستن</button>
                        <button type="button" class="btn btn-danger">حذف</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection

@section('script')
    <script>
        $('#delete').on('show.bs.modal',function (event) {
            var button = $(event.relatedTarget);
            var category = button.data('category');
            var modal = $(this);
            modal.find('.modal-body #categoey').val(category);
        });
    </script>
@endsection

web.PHP

Route::resource('categories','CategoryController');

CategoryController.PHP

public function destroy(Category $category)
{
    $category->delete();
    return redirect()->route('categories.index');
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)