step by step how to redirect user based on role in laravel code example

Example 1: how to redirect a particular user role to a page after login laravel

public function handle($request, Closure $next, $guard = null) {  if (Auth::guard($guard)->check()) {    $role = Auth::user()->role;     switch ($role) {      case 'admin':         return redirect('/admin_dashboard');         break;      case 'seller':         return redirect('/seller_dashboard');         break;       default:         return redirect('/home');          break;    }  }  return $next($request);}

Example 2: how to redirect a particular user role to a page after login laravel

<?phpnamespace App\Http\Middleware;use Closure;use Illuminate\Auth\Middleware\Role as Middleware;use Illuminate\Support\Facades\Auth;class Role {  public function handle($request, Closure $next, String $role) {    if (!Auth::check()) // This isnt necessary, it should be part of your 'auth' middleware      return redirect('/home');    $user = Auth::user();    if($user->role == $role)      return $next($request);    return redirect('/home');  }}

Tags:

Php Example