Saturday, March 18, 2017

Laravel 5.4 change user role

I trying make changing role in my panel admin. But when i made it my form dont sending post request. Now only working showing user role. I cant fix it becouse when i click button to switch i dont get any error and role is not changing.

I use HttpRequester when i use url /admin/change its showing this error: MethodNotAllowedHttpException in RouteCollection.php line 233

There is my code:

View:

@extends('layouts.app')
@section('content')
<h2>Panel Admina</h2>
<div class="panel-group">
        <div class="panel panel-default">
          <div class="panel-body">
                <table class="table">
           <thead>
           <tr>
            <th>Id</th>
      <th>Imię</th>
      <th>Nazwisko </th>
      <th>E-mail </th>
      <th>Nr.tele </th>
      <th>Użytkownik </th>
      <th>Moderator </th>
      <th>Admin </th>
</tr>     
</thead>
<tbody>
@foreach($users as $user)
  <tr>
    <form action="" method="post">

                
      <td></td>
      <td></td>
      <td></td>
      <td><input type="hidden" name="email" value=""></td>
      <td></td>
      <td><input type="checkbox"  name="role_user"/></td>
      <td><input type="checkbox"  name="role_moderator"/></td>
      <td><input type="checkbox"  name="role_admin"/></td>
      <td><input type="submit" class="btn btn-default btn-sm" value="Przydziel rolę"></td>


    </form>
  </tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>

@endsection

Controller:

  public function change(Request $request)
    {
        $user = User::where('email', $request['email'])->first();

        $user->roles()->detach();

        if ($request['role_user']) {
            $user->roles()->attach(Role::where('name', 'User')->first());
        }
        if ($request['role_moderator']) {
            $user->roles()->attach(Role::where('name', 'Moderator')->first());
        }
        if ($request['role_admin']) {
            $user->roles()->attach(Role::where('name', 'Admin')->first());
        }
        return redirect()->back();
    }

Routing:

Route::get('/admin', [
        'uses' => 'AdminController@index',
        'middleware' => 'roles',
        'roles' => ['Admin']
    ]);

Route::post('/admin/change', [
    'uses' => 'AdminController@change',
    'as' => 'change',
    'middleware' => 'roles',
    'roles' => ['Admin']
]);

I really dont know how i can resolve my problem.



via Mariusz

Advertisement