Skip to content
Advertisement

Delete information from database using laravel 6.0 and function destroy doesnt work [closed]

I am trying to eliminate an event that I created before. I tried to use the function destroy() but it didn’t work. I think that the cause could be the route in the view

<!--Here it is the code of the view that has the button delete-->
@foreach ($eventos as $item)
  <tr>
    <td>{{$item->name_evento}}</td>
    <td>{{$item->date_evento}}</td>
    <td>{{$item->user}}</td>
    <td>
      <form action="/eventos/destroy" method="DELETE" class="d-inline">                 
        <button class="btn btn-dark btn-sm" type="submit">Delete</button>
      </form>
    </td>
  </tr>
@endforeach
<!--Here it is the code of my function destroy() in EventoController-->
public function destroy($id)
{
    $eventos=Evento::findOrFail($id);
    $eventos->delete();
    return back()->with('menssage','sucesfully deleted');
}
<!--And here is the route code-->
Route::resource('/eventos', 'EventoController');

Advertisement

Answer

You have problem in your code. Try below code.

Your view:

@foreach ($eventos as $item)
     <tr>
         <td>{{$item->name_evento}}</td>
         <td>{{$item->date_evento}}</td>
         <td>{{$item->user}}</td>
         <td>
           <form action="{{ route('eventos.destroy', ['evento' => $item]) }}" method="POST" class="d-inline">
             @csrf
             @method('DELETE')
             <button class="btn btn-dark btn-sm" type="submit">Delete</button>
           </form>
         </td>
     </tr>
@endforeach

EventoController:

public function destroy(Evento $evento)
{
    $evento->delete();
    return back()->with('message','successfully deleted');
}

Routes:

Route::resource('/eventos', 'EventoController');
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement