Home > Blockchain >  Deleting with eloquent return, but nothing is deleted
Deleting with eloquent return, but nothing is deleted

Time:03-09

When i try to delete something nothing happen but i get the return. I tried to to use $id instead of Game $game on destroy method, but it didn't work also.

Route:

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

Controller:

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

View:

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

CodePudding user response:

The destroy function in your controller requires an 'id' as a parameter. That means you have to supply an id parameter on your path as in the following:

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

CodePudding user response:

I changed my function destroy, it stayed that way:

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

But I don't know yet why the other method didn't work.

  • Related