Monday, April 10, 2017

How get id from other user in Laravel

I am trying to create a user manager in Laravel, I have implemented methods for editing and deleting registered users in my database, but when I try to delete a selected user, it picks up the id of the logged in user, thus excluding the logged in user account.

Function Destroy from Admin Controller

public function destroy($id)
  {
    $user = User::find($id);

    $user->delete($id);
  }

View from Admin index

@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">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Cadastro de usuários</h4>
            </div>
            <div class="modal-body">
              <form class="" action="" method="post">
                
              <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="" alt="" style="width: 75px; border-radius: 50%;"> </td>
         <td></td>
         <td></td>
         <td></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">&times;</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>

@endsection

Route

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

Any suggestion?



via Vinicius Rosa

Advertisement