Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devhoussam123's avatar

Laravel Spatie Permissions - it display unauthorized role

Image of Example

as you can see in this image it should not displayed the "Sponsor" only the project as role name.

RegisteredUserController

public function store(Request $request)
    {
    $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|confirmed|min:8',
    ]);

    Auth::login($user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]));
		
	// Create roles
		
	$sponsorRole = Role::create(['name' => 'sponsor']);
	$projectRole = Role::create(['name' => 'project']);
		
	// Assigning roles
		
	$user->assignRole($sponsorRole);
	$user->assignRole($projectRole);
		
	// Check if it's correct roles
		
	if(auth()->user()->getRoleNames() && auth()->user()->hasRole('sponsor')) {
		return redirect()->intended('/sponsor/dashboard');
	} else if (auth()->check() && !auth()->user()->hasRole('sponsor')) {
		return redirect()->back();
	}
		
	if(auth()->user()->getRoleNames() && auth()->user()->hasRole('project')) {
		return redirect()->intended('/project/dashboard');
	} else if (auth()->check() && !auth()->user()->hasRole('project')) {
		return redirect()->back();
	}
		
	event(new Registered($user));
}

AuthenticatedSessionController

    public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();
		
		if(auth()->check() && auth()->user()->hasRole('sponsor')) {
			return redirect()->intended('/sponsor/dashboard');
		}
		
		if(auth()->check() && auth()->user()->hasRole('project')) {
			return redirect()->intended('/project/dashboard');
		}
		
		return redirect(RouteServiceProvider::HOME);
    }

test.blade.php

   @role('project')
			Welcome! <strong>{{ Auth::user()->name }}</strong> You're logged in! as 
			<strong>{{ Auth::user()->getRoleNames() }}</strong>
    @else
			<strong>I'm not a project...</strong>
	@endrole
0 likes
2 replies
jlrdw's avatar
You're logged in as {{ $role }}

However get the role into the variable and pass to view as well.

Edit: Also please clean up your previous questions. If any answers helped, mark as answered. This will also help others.

devhoussam123's avatar

this not help my friend I'm not looking for How to display the role of the user.

please read this well :

*** Scenario 1 ***

let try that I visit Udemy E-learning platform and I prefer to Create an account as "INSTRUCTOR" after complete the registration with specified fields it should redirect me to specified dashboard made only for this type of users.

*** Scenario 2 ***

let try that I visit Udemy E-learning platform and I prefer to Create an account as "STUDENT" after complete the registration with specified fields it should redirect me to specified dashboard made only for this type of users.

*** Here is my updated code ***

*** RegisteredUserController ***

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|confirmed|min:8',
        ]);

        Auth::login($user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]));
		
		
		$studentRole = Role::create(['name' => 'student']);
		$instructorRole = Role::create(['name' => 'instructor']);
		
		
		$user->assignRole($studentRole);
		$user->assignRole($instructorRole);
		
		
		if(auth()->user()->getRoleNames() && auth()->user()->hasRole('student')) {
			return redirect()->intended('/student/dashboard');
		} else if (auth()->check() && !auth()->user()->hasRole('student')) {
			return redirect()->back();
		}
		
		if(auth()->user()->getRoleNames() && auth()->user()->hasRole('instructor')) {
			return redirect()->intended('/instructor/dashboard');
		} else if (auth()->check() && !auth()->user()->hasRole('instructor')) {
			return redirect()->back();
		}
		
		event(new Registered($user));
    }

*** AuthenticatedSessionController ***

    public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();
		
		if(auth()->check() && auth()->user()->hasRole('student')) {
			return redirect()->intended('/student/dashboard');
		}
		
		if(auth()->check() && auth()->user()->hasRole('instructor')) {
			return redirect()->intended('/instructor/dashboard');
		}
		
		return redirect(RouteServiceProvider::HOME);
    }

Please or to participate in this conversation.