I am deleting a record and getting the above error but when I go back the record is deleted. Why I am getting an error if the record is deleted when I go back? I tried solving like this: this is my view blade page route
<a href='/DeleteCandidateID/{{$candidate_id->id}}'><i class="fas fa-trash fa-lg text-danger"></i></a>
I tried this as well:
form method as well but getting same answer
and this is my web.php
Route::post('/DeleteCandidateID/{CanDelID}', 'CandidateController@DeleteCandidateID')->name('candidate.DeleteCandidateID');
this is my controller :
public function DeleteCandidateID($CanDelID) {
$canid = Candidate::findOrFail($CanDelID)->delete();
return redirect()->back()->with('success', 'candidate deleted successfully');
}
CodePudding user response:
FIRST POSSIBILITY :
You added a simple link and in your routes file you indicated that you are waiting a POST request, so you need to add a form in your blade view with the POST method:
<form method="POST" action="{{ route('candidate.DeleteCandidateID', ['CanDelID' => $candidate_id->id]) }}">
<button type="submit">Delete the candidate</button>
@csrf
</form>
SECOND POSSIBILITY :
Following this post, if the problem still persists, you can try to keep your link :
<a href='/DeleteCandidateID/{{$candidate_id->id}}'><i class="fas fa-trash fa-lg text-danger"></i></a>
And change Route::post(...)
to Route::get(...)
. I think it can fix your problem but it's definilty not the best way.
THIRD POSSIBILITY :
Overide the POST method with method_field
:
<form method="POST" action="{{ route('candidate.DeleteCandidateID', ['CanDelID' => $candidate_id->id]) }}">
<button type="submit">Delete the candidate</button>
@csrf
{{ method_field('DELETE') }}
</form>
And change your route to : Route::delete(...)
CodePudding user response:
- Your route should use
delete
method on your route. Run php artisan route:list to confirm that.
Route::Delete('/DeleteCandidateID/{CanDelID}','CandidateController@DeleteCandidateID')->name('candidate.DeleteCandidateID');
Use a form to delete and Laravel will interpret this second argument
['CanDelID' => $candidate_id->id]
the way you've written it as as a query string so replace it with the id of the candidate. Your migration will help to see how you've labelled that if you can provide it.<form method="POST" action="{{ route('candidate.DeleteCandidateID', $idOfTheCandidate) }}"> @csrf @method('delete') <button type="submit">Delete the candidate</button> </form>
That should work and note
@method('delete')
in the form. Browsers do not understand delete an put methods, so we place it there and it will override post method when the server is hit.