I have been doing an CRUD
en Laravel
and it is giving me an error that cannot be located since I do not have any function that is called Show
.
Users controller:
public function destroy($id_usuario)
{
$user = Usuario::find($id_usuario);
$user->delete();
Flash::warning('Se ha eliminado ' .$user->nombre . 'de forma correcta');
return redirect()->route('users.index');
}
Index.blade.php
<a href="{{ route('users.destroy', $user->id_usuario) }}" class="btn btn-warning"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true" onclick="return confirm('¿Seguro que quieres borrarlo')"></span></a>
Web.php
Route::group(['prefix' => 'admin'], function(){
Route::resource('users','Userscontroller');
Route::get('users/{id_usuario}/destroy', [
'uses' => 'UsersController@destroy',
'as' => 'admin.users.destroy'
]);
});
When you use
Route::resource()
you are creating seven routes to said controller:Each of these methods uses a specific verb and method in the controller, as the example in the documentation at
PhotoController
.What happens in this case is that when you click on the link with route
users/{id_usuario}/destroy
and GET verb, Laravel is finding the show route/users/{id_usuario}
generated byRoute::resource()
before the one you created.Why is this happening? Routes in Laravel are read in "order" from top to bottom, and upon finding a verb match and if the "partial" or full parameter matches the route, it will use it.
In other words, when you click the link, Laravel checks GET and then finds a match on the route that calls the show method thanks to
users/{id_usuario}
, and the rest of the route (.../destroy
) is ignored.SOLUTIONS:
Use the DELETE verb when doing a destroy as suggested by Laravel, for this you would make a call to the link with a form, something like this:
Reorder the routes so that Laravel finds your route first than the resource route:
Tell Laravel not to generate the route that the show method uses:
More information in the documentation: https://laravel.com/docs/5.4/controllers#resource-controllers