if user role is anything but admin php code example

Example: php how to check if user has a role on login

// in users Model
public function hasRole($role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return true;
        }
        // else
        return false;
    }

// In login controller
// first comment out default laravel home redirect

// protected $redirectTo = RouteServiceProvider::HOME;

// then, redirect user(s) based on their role
    protected function authenticated(Request $request, $user)
    {
        if ($user->hasRole('administrator')) {
            return redirect()->route('users.admin.index');
        }

        if ($user->hasRole('user')) {
            return redirect()->route('home');
        }
    }

Tags:

Php Example