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
JavaScript
x
<!--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
JavaScript
<!--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:
JavaScript
@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:
JavaScript
public function destroy(Evento $evento)
{
$evento->delete();
return back()->with('message','successfully deleted');
}
Routes:
JavaScript
Route::resource('/eventos', 'EventoController');