Skip to content
Advertisement

Confirm delete using sweetalert in Laravel Form Collective

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 IlluminateHttpResponse
     */
    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:xampphtdocsWebsys_2_ProjectresourcesviewsAnnouncementsFolderdelete_scripts.blade.php)

Advertisement

Answer

Change

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

to

url: "/announcements/" + delete_id,`
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement