I'm a beginner in using laravel, please help.
I'm using sweet alert to ask for confirmation before deleting. The error says that it is missing a required parameter for destroy, though I am certainly sure that I am passing an id to the function destroy.
Here's my code
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>
destroy function in 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
Route::resource('announcements', 'AnnouncementsController');
The error is this:
Missing required parameters for [Route: announcements.destroy] [URI: announcements/{announcement}]. (View: C:\xampp\htdocs\Websys_2_Project\resources\views\AnnouncementsFolder\delete_scripts.blade.php)
My Answer:
__@endsection__
@section('content')
Announcements
Title | Action |
---|---|
{{ $announcement->title }} | {!! Form::open(['method' => 'DELETE', 'route' => ['announcement.destroy', $announcement->id], 'style'=>'display:inline']) !!} {!! Form::close() !!} |
@endsection
@section('scripts')
@endsection
In your controller, make sure you have the destroy method defined like this:
public function destroy($id)
{
// Find the announcement by id and delete it
Announcement::find($id)->delete();
return redirect()->route('announcement.index')->with('success', 'Announcement deleted successfully');
}
Make sure you have the Announcement model imported at the top of your controller file:
use App\Models\Announcement;
Also, make sure you have the necessary routes defined in your routes file:
Route::resource('announcement', 'AnnouncementController');
With these changes, your delete confirmation using SweetAlert should work correctly. Let me know if you need further assistance.
Rate this post
3 of 5 based on 4007 votesComments