在 Laravel Form Collective 中使用 sweetalert 确认删除
Confirm delete using sweetalert in Laravel Form Collective
我是初学者laravel,请帮忙
我正在使用甜蜜警报在删除前要求确认。该错误表明它缺少销毁所需的参数,但我确信我正在将 id 传递给函数销毁。
这是我的代码
Announcement.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 \Illuminate\Http\Response
*/
public function destroy($id)
{
$announcements = Announcement::find($id);
$announcements->delete();
return redirect('/announcements');
}
路线
Route::resource('announcements', 'AnnouncementsController');
错误是这样的:
[路由:announcements.destroy][URI:announcements/{announcement}] 缺少必需的参数。 (视图: C:\xampp\htdocs\Websys_2_Project\resources\views\AnnouncementsFolder\delete_scripts.blade.php)
改变
url: "{{ route('announcements.destroy') }}" + '/' + delete_id,`
至
url: "/announcements/" + delete_id,`
我是初学者laravel,请帮忙
我正在使用甜蜜警报在删除前要求确认。该错误表明它缺少销毁所需的参数,但我确信我正在将 id 传递给函数销毁。
这是我的代码
Announcement.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 \Illuminate\Http\Response
*/
public function destroy($id)
{
$announcements = Announcement::find($id);
$announcements->delete();
return redirect('/announcements');
}
路线
Route::resource('announcements', 'AnnouncementsController');
错误是这样的:
[路由:announcements.destroy][URI:announcements/{announcement}] 缺少必需的参数。 (视图: C:\xampp\htdocs\Websys_2_Project\resources\views\AnnouncementsFolder\delete_scripts.blade.php)
改变
url: "{{ route('announcements.destroy') }}" + '/' + delete_id,`
至
url: "/announcements/" + delete_id,`