TheBAST
TheBAST

Reputation: 2736

Laravel Destroy Option not deleting

Why do this occurs? This is my index lists of all accounts. I want to just delete a specific category by that category.destroy route but it's

index.blade.php

@extends('layouts.master')
@section('title','All Categories')
@section('contents')
<div class="row">
    <div class="col-md-8 col-sm-4 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">All Categories</div> 
            <div class="panel-body">
                <article>
                    <div class="table-responsive-vertical shadow-z-1">
                        <!-- Table starts here -->
                        <table id="table" class="table table-hover table-mc-light-blue">
                            <thead>
                                <tr>
                                    <th>ID No</th>
                                    <th>Category</th>
                                    <th>Edit/Delete</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            @foreach($categories as $category)
                            <tbody>
                                <tr>
                                    <td data-title="ID">{{$category->id}}</td>
                                    <td data-title="Name">{{$category->name}}</td>
                                    <td><a href="{{ route('category.edit',$category->id)  }}" class="btn btn-primary btn-sm pull-left">Edit</a>
                                        &nbsp<a href="{{ route('category.destroy', $category->id)  }}" class="btn btn-danger btn-sm">Delete</a>
                                    </td>

                                </tr>         
                            </tbody>
                            @endforeach
                        </table>
                    </div>
                </article>

            </div>
        </div>
    </div>
</div>
@endsection
@section('js') 
{!!Html::script('assets/js/jquery.min.js')!!} 
{!!Html::script('assets/js/bootstrap.min.js') !!}
<script>
    $('#flash-overlay-modal').modal();
</script>

<script>
    $('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>
@endsection

CategoryController.php

public function destroy($id){
        $category = Category::findOrFail($id);
        $category->delete();
        Session::flash('flash_message', 'Task successfully deleted!');
        return redirect()->route('category.index');
    }

instead it just displays the view specific entry of the category. It's not deleting or something

Upvotes: 1

Views: 167

Answers (2)

patricus
patricus

Reputation: 62278

To access your destroy route, you have to use the DELETE HTTP request verb. HTML links only allow GET requests.

You should either change your HTML link to an HTML form that spoofs the DELETE method, or look into using something like restfulizer.js that will automatically convert your delete links to delete forms.

As has been suggested, you could also create a GET route for the delete functionality, but there are potential consequences to this. GET and HEAD requests should generally be thought of as "read only" requests, and should not modify any data. POST, PUT, PATCH, and DELETE requests are generally thought of as "write" requests. A web spider may crawl your delete links and end up deleting all your data, or a web browser may pre-fetch all the GET requests on a page, so the delete link is accessed even though no one clicked the delete button. There are a lot of potential nasty things that can happen when you start allowing GET requests to modify data. There is some good information in this answer.

Upvotes: 2

maudev
maudev

Reputation: 1101

Try with this route:

Route::get('category/{category}/destroy',[
  'uses'=>'CategoryController@destroy',
  'as' => 'category.destroy'
]);

Upvotes: 1

Related Questions