我正在尝试消除我之前创建的事件。我尝试使用功能destroy()
,但是没有用。我认为原因可能是视图中的路线
<!--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');
答案 0 :(得分:0)
您的代码有问题。尝试下面的代码。
您的视图:
@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');
}
路线:
Route::resource('/eventos', 'EventoController');