【发布时间】:2020-09-07 22:07:17
【问题描述】:
我想使用模态删除数据,我已经获得了要删除的数据的 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-action="{{ route('categories.destroy', $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">×</span>
</button>
</div>
<div class="modal-body">
آیا از حذف {{ $category->name }} اطمینان دارید؟
<input type="hidden" id="category" name="category">
</div>
<div class="modal-footer">
<button type="button" class="btn bg-white" data-dismiss="modal">بستن</button>
<button type="submit" class="btn btn-danger" data-action="{{ route('categories.destroy', $category->id) }}" data-toggle="modal" data-target="#deleteCategory">حذف</button>
</div>
</div>
</form>
</div>
</div>
@endsection
@section('script')
<script>
$('#deleteCategory').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var modal = $(this);
modal.find('form').attr('action', action);
});
</script>
@endsection
CategoryController.php
public function destroy(Category $category)
{
dd($category);
}
web.php
Route::resource('categories', 'CategoryController');
我想去CategoryController.php中的destroy方法。
它甚至没有显示dd。
【问题讨论】:
-
请求类型在你的 ajax 函数中应该是 DELETE
type: 'DELETE',。如果您在控制台中运行php artisan route:list,您可以看到所有路由以及您需要发出的请求类型以在后端访问该特定功能 -
@JairoNavaMagaña 我更新了我的帖子
标签: laravel-5.8