David Orizu
David Orizu

Reputation: 27

How can I delete this Laravel form using get method

The 'Delete' method was not working so I used 'get' but my data is not deleting

<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>
               
  public function destroy($id)
  {
    dd($id);
    $student = Student::where('id',$id)->delete();
    return redirect('/data/{id}')->with('success', 'Data Deleted');
  }
}

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

Upvotes: 0

Views: 1022

Answers (2)

David Orizu
David Orizu

Reputation: 27

This is I finally got the answer I ran the following command:

php artisan route:clear
php artisan route:list

Then it gave me suggested methods and delete method was shown for "Controller@destroy" route then I changed from "get" method to "delete" method. Then, my post was able to delete.

Upvotes: 1

Ali Waqas
Ali Waqas

Reputation: 335

The form must be submitted via delete method

<form method="POST" action="{{ url('/data', $row['id']) }}"> 
    @method('DELETE')
    @csrf
  <button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
</form>

More details can be found here: https://laravel.com/docs/8.x/routing#form-method-spoofing

Upvotes: 0

Related Questions