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

Soo's avatar
Level 2

permission users

hi, i have crud for users and crud for roles, i have three roles admin, super admin and human ressource i want to hide the modul user interfaces for the human ressource user . Any help please i use laravel 8. PS: i don't have permission user table i just have table User and table Roles.

0 likes
25 replies
tykus's avatar

Write a Gate, e.g.

Gate::define('view_user_interfaces', function (User $user) {
	return in_array($user->role, ['admin', 'super_admin']);
});

Then anywhere that needs checking:

// Controller
$this->authorize('view_user_interfaces');
// or
if (auth()->user()->can('view_user_interfaces')) {
	// ...
}

// View Template
@can('view_user_interfaces')
	<!-- modal button -->
@endcan

This assumes you get the User's role as a string, if working with a Role instance (with a name) then:

return in_array($user->role->name, ['admin', 'super_admin']);
Soo's avatar
Level 2

@tykus Thank you for your answer, where i put this line.

Gate::define('view_user_interfaces', fn (User $user) {
	return in_array($user->role, ['admin', 'super_admin']);
});
tykus's avatar

You can add it to the boot method in app/Providers/AuthServiceProvider.php

1 like
Soo's avatar
Level 2

@tykus i get this error. ParseError syntax error, unexpected token "{", expecting "=>"

  public function boot()

    {

        $this->registerPolicies();

 

        Gate::define('view_user_interfaces', fn (User $user)

        {

        return in_array($user->role->name, ['Admin', 'Super Admin']);

       });

    }

}
tykus's avatar

Sorry, fixed above. I started writing a short Closure, and changed to a long format:

Gate::define('view_user_interfaces', function (User $user) {
    return in_array($user->role->name, ['Admin', 'Super Admin']);
});

or

Gate::define('view_user_interfaces', fn (User $user) => in_array($user->role->name, ['Admin', 'Super Admin']));
1 like
Soo's avatar
Level 2

@tykus That work , but i get now this error

Error Call to a member function can() on null in UserController

tykus's avatar

Do you have an authenticated user; it would seem not?

tykus's avatar

How are you using can inside the UserController action?

1 like
Soo's avatar
Level 2

@tykus i'm using can inside function index in the user controller.

tykus's avatar

Can you actually show the code?

1 like
Soo's avatar
Level 2

@tykus this is the code

public function index(User $user)
    {
        
        if (auth()->user()->can('view_user_interfaces', $user)) {
        $nbr = DB::table('users')->count();
        $users = User::with('role')->get();
        return view('users.index' , [ 'users'=> $users]);
        }
    }
automica's avatar

@soo this line

$nbr = DB::table('users')->count();

is redundant.

Soo's avatar
Level 2

@automica this line count how many users . show the number of users in the interface. why he is redundant? :(

automica's avatar

@soo because you arent passing it to the view.

if you want to, add it to

  return view('users.index' , [ 'users'=> $users,'nbr'=> $nbr]);
automica's avatar

how are you using $nbr in your blade if you arent passing that in at the end of your method?

Soo's avatar
Level 2

@automica yes but he work for me he show how much i have users . this is not the problem. the problem is permission not work for me :'(

tykus's avatar

Error Call to a member function can() on null in UserController

if (auth()->user()->can('view_user_interfaces', $user)) {

You don't have an authenticated user.

automica's avatar
automica
Best Answer
Level 54

it means you arent logged in.

if (auth()->user()

relies on auth() but as you're not logged in you can't check it.

you can add an addition check for auth and bail your method if its not present,

public function index(User $user)
    {
		if(!auth()){
			return redirect('/'); // bail if you arent logged in.
		}
        
        if (!auth()->user()->can('view_user_interfaces', $user){
			return redirect('/'); // bail if you don't have permission;

		}
	
        $users = User::with('role')->get();
        return view('users.index' , [ 'users'=> $users]);
  
    }
1 like
Soo's avatar
Level 2

hello @tykus , he show me this error.

TypeError
App\Providers\AuthServiceProvider::App\Providers\{closure}(): Argument #1 ($user) must be of type App\Providers\User, App\Models\User given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 477
freemium's avatar

is this is from spatie role permission package

Please or to participate in this conversation.