php how to check if user has a role on login

Solutions on MaxInterview for php how to check if user has a role on login by the best coders in the world

showing results for - "php how to check if user has a role on login"
Janice
27 Mar 2017
1// in users Model
2public function hasRole($role)
3    {
4        if ($this->roles()->where('name', $role)->first()) {
5            return true;
6        }
7        // else
8        return false;
9    }
10
11// In login controller
12// first comment out default laravel home redirect
13
14// protected $redirectTo = RouteServiceProvider::HOME;
15
16// then, redirect user(s) based on their role
17    protected function authenticated(Request $request, $user)
18    {
19        if ($user->hasRole('administrator')) {
20            return redirect()->route('users.admin.index');
21        }
22
23        if ($user->hasRole('user')) {
24            return redirect()->route('home');
25        }
26    }