简体   繁体   中英

Small admin control panel code on Laravel

I'm trying to develop a small control panel for admin to distribute roles to other users, the thing is my code doesn't show me any error but at the same time it doesn't affect any roles to other users. it stays just the same way, my control panel is based on submit methode automatically hen i check the role for a specific user.

my view admin code that contains control panel is;

      <div class="row offset-2">

           <div class="col-md-4 course_box">


   <h4>Panel de Controle </h4><br>
   <h6> <b>Liste des utilisateurs :</b></h6>
    <br><br>
   <div>
    <table class="table table-hover">
        <tr>
            <th>#</th>
            <th>Nom</th>
            <th>Email</th>
            <th>Elève</th>
            <th>Enseignant</th>   
            <th>Parent</th>   
            <th>Administrateur</th>
            </tr>
            @foreach($users as $user)
             <form method="post" action="/add-role">

           {{ csrf_field() }}


           <input type="hidden" name="email" value="{{ $user->email }}">
            <tr>
                  <th>{{ $user->id }}</th>
                  <td>{{ $user->name }}</td>
                   <td>{{ $user->email }}</td>
                    <td>
                        <input type= "checkbox" name='role_elève' onChange="this.form.submit()" {{ $user->hasRole('Elève') ? 'checked' : ' ' }}>  </td>
                     <td> <input type= "checkbox" name='role_ens' onChange="this.form.submit()" {{ $user->hasRole('Enseignant') ? 'checked' : ' ' }}></td>
                      <td> <input type= "checkbox" name='role_parent'  onChange="this.form.submit()" {{ $user->hasRole('Parent') ? 'checked' : ' ' }}></td>
                       <td> <input type= "checkbox" name='role_admin'  onChange="this.form.submit()" {{ $user->hasRole('Admin') ? 'checked' : ' ' }}></td>
                   </tr>
               </form>
                   @endforeach
                   </table>
                   </div>

          </div></div>

web.php contains the route add-role below:

  Route::post('add-role', [ 
  'uses' => 'PostsController@addRole',
   'as' => 'admin',
    'middleware' => 'roles',
      'roles' => ['Admin']
   ]);

addRole method in PostsController implements the code below:

   Public function addRole(Request $request) {

        $user = User::where('email', $request['email'])->first();
        $user->roles()->detach();

      if($request['role_elève'])
      {
         $user->roles()->attach(Role::where('name', 'Elève')->first());

      }

       if($request['role_ens'])
      {
         $user->roles()->attach(Role::where('name', 'Enseignant')->first());

      }

       if($request['role_parent'])
      {
         $user->roles()->attach(Role::where('name', 'Parent')->first());

      }

    if($request['role_admin'])
      {
         $user->roles()->attach(Role::where('name', 'Admin')->first());

      }

      return redirect()->back();
    }

I can't find the error when laravel doesn"t detect the details of it, any help would be appreciated alot

Your $request is object, not array...

Therefore, to make it work, change all your $request[ declarations to $request-> in your PostController, like in this example:

public function addRole(Request $request) {

$user = User::where('email', $request->email)->first();
$user->roles()->detach();

if($request->role_elève) {
    $user->roles()->attach(Role::where('name', 'Elève')->first());
}

if($request->role_ens) {
   $user->roles()->attach(Role::where('name', 'Enseignant')->first());
}

if($request->role_parent) {
   $user->roles()->attach(Role::where('name', 'Parent')->first());
}

if($request->role_admin) {
   $user->roles()->attach(Role::where('name', 'Admin')->first());
}

return redirect()->back();
}

Note:

I would suggest also, to change variable name of role_elève to role_eleve . If so, remember to modify fields in your view accordingly...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM