Laravel "delete does not exist."

0

I'm having an error when I try to delete an element from my bank. All other actions to insert are working. .

Ihaveanactionbuttonthatshouldpayanexamofmytable

Itappearsthatthedeletemethoddoesnotexist.

Thisismycontroller

Therouteiscorrect,becausetheotherfunctionsofthecrudarenormal.

Rout:list

IfoundanerrorsimilartominebutIdidnotunderstandhowitshouldapplytomyproject.I'vebeenplayinglaravelforonly1month.

Similarerror: link

    
asked by anonymous 18.07.2018 / 20:01

4 answers

0

In order for Route Model Binding to work, that is, the template is loaded and loaded based on the id passed in the URL and passed as a parameter to the controller method, it is necessary that the URL segment and variable name in the method be the same .

In the case of the route you have test/{test} and 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 bad I think it should be

action=“{{route(‘tests.destroy’, [$e->id])}}”
    
18.07.2018 / 20:24
0

Instead 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 instance of the Model

And in the method signature on the Controller

public function destroy(Test $test){ }
    
18.07.2018 / 21:23
0

Divide answered!

Resolution: In the action of form the second parameter is an array you are passing a string should put [$ e-> id] -obs: I was passing with single quotation marks.

Credit: @Jorge Costa

Thanks to all, all comments were of good value !!

    
19.07.2018 / 01:06
0

You can not remove because you pass only the id but it 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
    
18.07.2018 / 22:51