带着雄辩的回报删除,但什么都没有删除

  • 本文关键字:删除 回报 laravel eloquent
  • 更新时间 :
  • 英文 :


当我试图删除某些内容时,什么都没有发生,但我得到了返回。我试着使用$id而不是Game$Game-on-destroy方法,但它也不起作用。

路线:

Route::group([
'prefix' => '/jogos',
'as' => 'games.'
],function (){
Route::get('/','AppHttpControllersGamesController@index') -> name('index');
Route::get('/cadastro','AppHttpControllersGamesController@create') -> name('create');
Route::post('/cadastro','AppHttpControllersGamesController@store') -> name('store');
Route::get('/editar/{id}','AppHttpControllersGamesController@edit') -> name('edit');
Route::patch('/editar/{id}','AppHttpControllersGamesController@update') -> name('update');
Route::delete('/','AppHttpControllersGamesController@destroy') -> name('destroy');
}); 

控制器:

public function destroy($id){
Game::destroy($id);
return redirect() -> route('games.index') -> with('success','Jogo excluído com sucesso');
}

视图:

<tbody>
@foreach($games as $game)
<tr>
<td class="col-3">{{ $game->name }}</td>
<td>{{ $game->description }}</td>
<td class="col-2">
<form action="{{ route('games.destroy',$game->id) }}" method="POST">
@csrf
@method('DELETE')
<a href="{{ route('games.edit',$game->id) }}" class="btn btn-sm btn-warning">Editar</a>
<button type="submit" class="btn btn-sm btn-danger">Apagar</button>
</form>
</td>
</tr>
@endforeach
</tbody>

控制器中的destroy函数需要一个"id"作为参数。这意味着您必须在路径上提供id参数,如下所示:

Route::delete('/{id}', ...);

我更改了我的函数destroy,它保持不变:

public function destroy($id){
Game::where('id',$id)->delete();
return redirect('/jogos') -> with('success','Jogo excluído com sucesso');
}

但我还不知道为什么另一种方法不起作用。

最新更新