使用Laravel Form Collective中的sweetalert确认删除



我是laravel的初学者,请帮忙。

我在删除之前使用甜蜜提醒请求确认。错误表明它缺少destroy所需的参数,尽管我确信我正在向函数destroy传递一个id。

这是我的代码

公告.blade.php

@extends('masterlayout')
@section('title')Announcement @endsection
@section('myHeader')
<div class="container myJumbotron">
<h1>ANNOUNCEMENTS</h1>
@if(count($announcements) > 0)
@foreach($announcements as $announce)
<div class="jumbotron" style="margin-top: 20px; padding: 20px">
<input type = "hidden" class = "deletebtn_id" value = "{{ $announce->id}}">
<p>{{$announce->announcement}}</p>
<small>Written on {{$announce->created_at}}</small>
<hr>
<a href="/announcements/{{$announce->id}}/edit" class="btn bg-OwnInfo">Edit</a>
{!!Form::open(['action' => ['AnnouncementsController@destroy',$announce->id], 'method' => 'POST', 'class' => 'float-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn bg-OwnDanger myDeletebtn'])}}
{!!Form::close()!!}
</div>
@endforeach
{{$announcements->links()}}
@endif
</div>
<div class="container">
<div class="float-right">
<a href="/announcements/create" class="btn bg-OwnSuccess">New Announcement</a>
</div>
</div>
@include('AnnouncementsFolder.delete_scripts')
@endsection

delete_script.blade.php

<script>
$(document).ready(function() {
$('.myDeletebtn').click(function(e) {
e.preventDefault();
delete_id = $(this).closest("div").find('.deletebtn_id').val();
data_id = delete_id
//alert(data_id);
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$.ajax({
type: "DELETE",
url: "{{ route('announcements.destroy') }}" + '/' + delete_id,
success: function() {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
}
});
} else {
swal("Your imaginary file is safe!");
}
});
});
});
</script>

AnnouncementsController.php 中的销毁函数

/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
$announcements = Announcement::find($id);
$announcements->delete();
return redirect('/announcements');
}

路由

Route::resource('announcements', 'AnnouncementsController');

错误如下:

缺少[Route:announces.destroy][URI:announces/{announce}]所需的参数。(视图:C:\examplep\htdocs\Websys_2_Project\resources\views\AnnouncementsFolder\delete_scripts.blade.php(

更改

url: "{{ route('announcements.destroy') }}" + '/' + delete_id,`

url: "/announcements/" + delete_id,`

最新更新