Laravel "delete does not exist."

Asked

Viewed 450 times

1

I’m making a mistake when I try to erase an element of my bank. All other insert actions are working. erro.

i have an action button that should pay for an exam of my table

tabela

Only it appears that the delete method does not exist.

delete

this is my controller

inserir a descrição da imagem aqui

The correct route ta run, because the other functions of the normal crud ta.

Rout:list inserir a descrição da imagem aqui

I found an error similar to mine but I didn’t understand how I should apply in my project. I’m dealing with only 1 month old.

Similar mistake: https://github.com/laravel/framework/issues/23985

  • Please send php Artisan route:list

  • Rout list: add no post

  • what is in the variable id, I know she’s a collection, but could you please place the contents of it?

  • What you have in the id variable is what appears in the above image table in Exam Cod, which would be the id of the exam which I want to delete

  • Tip: In the next questions send the code and errors as text, and not as image, in image it is difficult to try to catch the code to replicate the error.

  • Excuse me once I ask on the platform, but in case someone wants to do some testing I will make my github available with the project-> https://github.com/CarlosEduardo12/Projetos-Web. just add vendor folder

Show 1 more comment

4 answers

0

In place of

action="{{ route('test.destroy', '$test->id') }}"

Place

action="{{ route('test.destroy', $e) }}"

You are passing the entire Collection, when the delete method accepts only one Model instance

And the method signature in the Controller

public function destroy(Test $test){ }
  • I made this modification only with > e, and with > e->id and it didn’t work. Cod with the code in my git -> https://github.com/CarlosEduardo12/Projetos-Web/tree/master/Pratico_3

0

can not you remove pq vc ta passing only the id but does not specify in which model it will do this operation.

change this:

public function destroy(Test $id)
{
$id->delete();// como pego o registro correto se nao sei em qual Model buscar pelo ID?
}

for this:

public function destroy(Test $id){
 $test = Test::find($id);// pega a registro do Model Test pela ID 
 $test->delete(); // agora vc deleta o registro pq vc acessou ele corretamente.
 session()->flash('mensagem','Exame excluido com sucesso');
 return redirect()->route('test.index'); 
}//desta forma agora deveria funcionar
  • Switching to this way he redirects me to a page :Sorry, the page you are Looking for could not be found. I switched to use the Test model which is where the id I want to delete is! Image with the hole made-> img.

  • My git with the project if available to test https://github.com/CarlosEduardo12/Projetos-Web

0

Debt answered!

Resolution: In the form action the second parameter is an array you are passing a string you should put [$e->id] -Obs: was passing with simple quotes.

credit: @Jorge Costa

Thank you all, all the remarks were of good value!!

0


In order for Route Model Binding to work, that is, the model in question is loaded based on the id passed in the URL and passed as parameter to the controller method it is necessary that the segment of the URL and variable name in the method are equal.

In the case on the route you have test/{test} and in the method

public Function Destroy(Test $id)

this signature should be:

public Function Destroy(Test $test)

Check the documentation for Implicit Binding

The action of your form is also wrong I think it should be

action=“{{route(‘tests.destroy’, [$e->id])}}”
  • Using the same not found method error signature, but aprece: Sorry, the page you are Looking for could not be found.

  • See my response edited in the form part

  • this is my git with the project if you are available to test https://github.com/CarlosEduardo12/Projetos-Web

  • In the form action the second parameter is an array you are passing a string you should put [$e->id]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.