oxbir
2 weeks ago
142
4
Laravel

Delete Data

Posted 2 weeks ago by oxbir

I want to delete data using modal, I already get the id of the data I want to delete, but when I click the delete button in modal nothing happen.. Please tell me if something wrong.. Thank you very much.

blade and 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');
}

Please sign in or create an account to participate in this conversation.