使用laravel 6.0从数据库中删除信息,并且功能destroy不起作用

时间:2019-12-08 18:03:15

标签: php html laravel postgresql

我正在尝试消除我之前创建的事件。我尝试使用功能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');

1 个答案:

答案 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');