我正在使用Laravel-5.8进行Web应用程序项目。除删除外,CRUD的所有其他部分均正常工作。
Controller
public function destroy(Request $request, $id)
{
$group = HrHolidayGroup::find($id);
$group->delete();
Session::flash('success', 'Holiday Group deleted successfully.');
return redirect()->route('hr.holiday_groups.index');
}
路由/网络
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('holiday_groups', 'HrHolidayGroupsController');
});
index.blade.php
<tbody>
@foreach($groups as $key => $group)
<td>
{{$key+1}}
</td>
<td>
{{$group->group_name ?? '' }}
</td>
<td>
{{ $group->description ?? '' }}
</td>
<td>
@can('holiday_group_delete')
<a class="btn btn-xs btn-danger" data-toggle="modal" data-target="#confirm-delete{{ $group->id }}" data-original-title="Close">
span style="color:white;">{{ trans('global.delete') }}</span>
</a>
@endcan
<div class="modal fade" id="confirm-delete{{ $group->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Delete Holiday Group</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{route('hr.holiday_groups.destroy',['id'=>$group->id])}}" method="post">
{{ csrf_field() }}
<p>Are you sure you want to delete this Holiday Group?</p>
<div class="modal-header">
<h4>{{ $group->group_name }}</h4>
</div>
</form>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
</td>
</tr>
@endforeach
当我单击图中的模式形式上的Delete按钮时,什么也没发生。它只保留在屏幕上,不执行任何操作。
我该如何解决?
谢谢。
尝试一下
<form action="{{route('hr.holiday_groups.destroy',['id'=>$group->id])}}" method="post">
{{ csrf_field() }}
{{method_field('DELETE')}}
<p>Are you sure you want to delete this Holiday Group?</p>
<div class="modal-header">
<h4>{{ $group->group_name }}</h4>
</div>
<button type="submit" class="btn btn-danger">Delete</button>
</form>
public function destroy($id)
{
$group = HrHolidayGroup::find($id);
$group->delete();
Session::flash('success', 'Holiday Group deleted successfully.');
return redirect()->route('hr.holiday_groups.index');
}