我正在尝试在Laravel中创建一个用户管理器,我已经实现了在我的数据库中编辑和删除注册用户的方法,但是当我尝试删除所选用户时,它会获取登录用户的id,因此不包括已登录的用户帐户。
从管理员控制器销毁
的功能public function destroy($id)
{
$user = User::find($id);
$user->delete($id);
}
从管理员索引中查看
@extends('layouts.sidebar')
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="panel-heading">
<h2>Gerenciamento de usuários</h2>
<div class="panel-body">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#insertUser">
Adicionar Usuário
</button>
<!-- Modal -->
<div class="modal fade" id="insertUser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Cadastro de usuários</h4>
</div>
<div class="modal-body">
<form class="" action="{{ url('/admin/criausuario')}}" method="post">
{{ csrf_field() }}
<label for="nome">Nome</label>
<input type="text" class="form-control" id="nome" placeholder="Nome Completo" name="nome"></br>
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Email" name="email"></br>
<label for="senha">Senha</label>
<input type="password" class="form-control" id="senha" placeholder="Senha" name="senha"></br>
<label for="tipo">Tipo</label>
<select class="form-control">
<option value="0">Usuário</option>
<option value="1">Administrador</option>
</select>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<button type="submit" class="btn btn-default">Cadastrar</button>
</div>
<div class="modal-footer">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<table class="table">
<tr>
<th>Avatar</th>
<th>ID</th>
<th>Nome</th>
<th>Email</th>
<th>Ações</th>
</tr>
<tr>
@foreach($users as $user)
<td> <img src="{{$user->avatar}}" alt="" style="width: 75px; border-radius: 50%;"> </td>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td><a href="{!! url('admin/editar/'. $user->id) !!}" class="btn btn-primary" role="button"><i class="glyphicon glyphicon-edit"></i></a>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#confirmacao">
<i class="glyphicon glyphicon-remove"></i>
</button></td>
</tr>
<div class="modal fade" id="confirmacao" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Confirmação</h4>
</div>
<div class="modal-body">
Você tem certeza que deseja excluir o usuário selecionado?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
<a href="{!! url('/admin/deletar/'. $user->id) !!}"><button type="button" class="btn btn-danger">Excluir</button></a>
</div>
</div>
</div>
</div>
@endforeach
</table>
</div>
</div>
</div>
</div>
{{ $users->links()}}
@endsection
路线
Route::get('/admin', 'AdminController@index')->middleware('admin');
Route::get('/admin/editar/{id}', 'AdminController@edit')->middleware('admin');
Route::post('/admin/editar/{id}', 'AdminController@update')->middleware('admin');
Route::post('/admin/criausuario/', 'AdminController@store');
Route::get('/admin/deletar/{id}', 'AdminController@destroy')->name("user::deleteGet")->middleware('admin');
有什么建议吗?