David Orizu's avatar

How to delete laravel form using get method

Hello Guys! I'm just a laravel beginner and I would love your answers to be more clarified. I used delete method to delete a post on laravel framework but it threw an error saying, "Delete method not Supported" so I decided to use GET method and it threw no errors. Not withstanding, the issue is that it redirects without deleting anything not even showing message. This is my block of code executed below:

blade.php file

'''

{{ method_field('GET') }} {{ csrf_field() }} {{ trans('Delete') }} '''

controller

''' public function destroy($id) { dd($id); $student = Student::where('id',$id)->delete();

    return redirect('/data/{id}')->with('success', 'Data Deleted');
}

'''

route

''' Route::get('/data/{id}', 'App\Http\Controllers\StudentController@destroy'); '''

I joined laracast newly because I've seen that your questions and answers has really been of great help to me so I strongly believe that your responses would give me exactly what I'm looking for. Please I really need you guys help this has kept me on deck for 1 week now I seriously need solutions as fast as possible. Thank you.

0 likes
22 replies
Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Never delete using a GET request. You should post a form. Your route needs to be Route::delete

1 like
GeordieJackson's avatar

To delete a database entry you need to use "DELETE" request method rather than "GET" or "POST". e.g.:

Route::delete('users/{user}', 'UserController@destroy');

In your form, you need to set the form's "method" to "delete" but forms do not have a "delete" method available so you have to spoof it. So, in your form just add:

<input type="hidden" name="_method" value="DELETE">

Or as a shortcut, just add the blade directive:

@method('DELETE') 

instead.

See: https://laravel.com/docs/8.x/routing#form-method-spoofing

1 like
David Orizu's avatar

I tried this but it threw an error saying : DELETE Method not supported, suggested methods are GET, HEAD and PATCH

David Orizu's avatar

Index.blade.php

@extends('stud_view')

@section('content')

<div class="row">
    <div class="col-md-12">
        <br/>
        <h3 align="center">Student Data</h3>
        <br/>
        @if($message = Session::get('success'))
        <div class="alert alert-success" role="alert">
            <p>{{$message}}</p>
        </div>
        @endif
        <div align="right">
        <a href="{{url('student')}}" class="btn btn-primary">Add</a>
        <br/>
        <br/>
        </div>
        <table class="table table-bordered">
           <tr>
               <th>First Name</th>
               <th>Last Name</th>
               <th>Edit</th>
               <th>Delete</th>
           </tr>
           @foreach($students as $row)
           <tr>
               <td>{{$row['first_name']}}</td>
               <td>{{$row['last_name']}}</td>
              <td><a href="{{ url('student/'.$row['id'].'/edit') }}" class="btn btn-warning text-white">{{ trans('Edit') }}</a></td>
               <td>
                  <form method="post" class="delete_form" action="{{ url('/data',$row['id']) }}" id="studentForm_{{$row['id']}}">
                  {{ method_field('GET') }}
                  {{  csrf_field() }}
                  <button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
                  </form>
               </td>
           </tr>
           @endforeach
        </table>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
      $('.delete_form').on('submit', function(){
         if(confirm("Are you sure you want to delete it?"))
         {
             return true;
         }
         else
         {
             return false;
         }
      });
  });
</script>

@endsection

StudentsController.php

 public function destroy($id)
    {
        dd($id);
        $student = Student::where('id',$id)->delete();

        return redirect('/data/{id}')->with('success', 'Data Deleted');
    }
}

web.php

Route::get('/create', 'App\Http\Controllers\StudentController@create');

Route::post('/student', 'App\Http\Controllers\StudentController@store');

Route::get('/data', 'App\Http\Controllers\StudentController@index');

Route::get('/student', 'App\Http\Controllers\StudentController@create');

Route::get('/student/{id}/edit', 'App\Http\Controllers\StudentController@edit');

Route::post('/data/{id}', 'App\Http\Controllers\StudentController@update');

Route::get('/data/{id}', 'App\Http\Controllers\StudentController@index');

Route::get('/data/{id}', 'App\Http\Controllers\StudentController@destroy');

This is full code block

David Orizu's avatar

With this I think it's more clearer when I changed {{ method_field('GET') }} to {{ method_field('DELETE') }} it threw that error I was talking of. Please I know I'm really stressing you guys but let's try and see how we can improve on our responses

Sinnbeck's avatar

Your route is wrong.. It should be delete!

Route::delete('/data/{id}', 'App\Http\Controllers\StudentController@destroy');
David Orizu's avatar

I tried It now redirects without deleting anything and I don't know why

Sinnbeck's avatar

Do you still have the dd($id) in there? If not, add it back and see if it gets triggered. And are you certain that you still have {{ method_field('DELETE') }} ?

David Orizu's avatar

I just changed method field to 'DELETE' then, it threw an error: The DELETE method is not supported for this route. Supported methods: GET, HEAD, PATCH.

David Orizu's avatar
@extends('stud_view')

@section('content')

<div class="row">
    <div class="col-md-12">
        <br/>
        <h3 align="center">Student Data</h3>
        <br/>
        @if($message = Session::get('success'))
        <div class="alert alert-success" role="alert">
            <p>{{$message}}</p>
        </div>
        @endif
        <div align="right">
        <a href="{{url('student')}}" class="btn btn-primary">Add</a>
        <br/>
        <br/>
        </div>
        <table class="table table-bordered">
           <tr>
               <th>First Name</th>
               <th>Last Name</th>
               <th>Edit</th>
               <th>Delete</th>
           </tr>
           @foreach($students as $row)
           <tr>
               <td>{{$row['first_name']}}</td>
               <td>{{$row['last_name']}}</td>
              <td><a href="{{ url('student/'.$row['id'].'/edit') }}" class="btn btn-warning text-white">{{ trans('Edit') }}</a></td>
               <td>
                  <form method="post" class="delete_form" action="{{ url('/data',$row['id']) }}" id="studentForm_{{$row['id']}}">
                  {{ method_field('DELETE') }}
                  {{  csrf_field() }}
                  <button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
                  </form>
               </td>
           </tr>
           @endforeach
        </table>
    </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
      $('.delete_form').on('submit', function(){
         if(confirm("Are you sure you want to delete it?"))
         {
             return true;
         }
         else
         {
             return false;
         }
      });
  });
</script>

@endsection
Route::get('/create', 'App\Http\Controllers\StudentController@create');

Route::post('/student', 'App\Http\Controllers\StudentController@store');

Route::get('/data', 'App\Http\Controllers\StudentController@index');

Route::get('/student', 'App\Http\Controllers\StudentController@create');

Route::get('/student/{id}/edit', 'App\Http\Controllers\StudentController@edit');

Route::post('/data/{id}', 'App\Http\Controllers\StudentController@update');

Route::get('/data/{id}', 'App\Http\Controllers\StudentController@index');

Route::delete('/data/{id}', 'App\Http\Controllers\StudentController@destroy');
public function destroy($id)
    {
        dd($id);
        $student = Student::where('id',$id)->delete();

        return redirect('/data/{id}')->with('success', 'Data Deleted');
    }
}
David Orizu's avatar

How far? Have you been able to come up with something?

David Orizu's avatar

I've done that but the problem is still coming from the method. It's still showing : DELETE Method not supported, suggested methods are GET, HEAD and PATCH

automica's avatar
automica
Best Answer
Level 54

@david orizu can you run

php artisan route:clear;
php artisan route:list

and show results?

1 like
David Orizu's avatar

God Bless you, God bless Laracast. More wins more brains.

Please or to participate in this conversation.