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

RileyGWeb's avatar

500 error when accessing page with authorization functions on it?

Steps to reproduce would be:

Setup gate

// AuthServiceProvider.php
Gate::define('access-admin', function ($user) {
	return $user->hasRole('admin');
});

Use @can directive in blade template

@can('access-admin')
	// super secret stuff
@endcan

Login, navigate to page.

If I have admin permissions, all is well. If I do not, I get this error in my logs:

[2023-10-02 22:49:52] production.ERROR: Call to a member function handle() on null {"userId":5,"exception":"[object] (Error(code: 0): Call to a member function handle() on null at /home/forge/www.my-website.com/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php:60)
[stacktrace]

In case it helps, here's my role stuff in the User model:

public function roles()
{
    return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->withTimestamps();
}
public function hasRole($roleName)
{
    return $this->roles()->where('name', $roleName)->exists();
}

And in the Role model: (Haha. role model)

public function users()
{
    return $this->belongsToMany(User::class, 'role_user', 'role_id', 'user_id')->withTimestamps();
}

This only happens on production, as you can see from that error - works fine on local, which makes testing tough.

That's about all I can think of to provide context, let me know if there is anything else that might be useful. What could be causing this?

0 likes
2 replies
martinbean's avatar

@coaster132 Actually follow the stack trace to see where you’re calling a handle() method? Because whatever variable you’re calling it on, is null and not an object.

RileyGWeb's avatar

@martinbean Yeah good idea. It seems to be here:

#3 /home/forge/www.my-website.com/vendor/statamic/cms/src/Auth/Eloquent/User.php(99): Illuminate\Support\HigherOrderCollectionProxy->__call()
#4 /home/forge/www.my-website.com/vendor/statamic/cms/src/Auth/Eloquent/User.php(90): Statamic\Auth\Eloquent\User->getRoles()
#5 /home/forge/www.my-website.com/vendor/statamic/cms/src/Auth/Eloquent/User.php(222): Statamic\Auth\Eloquent\User->roles()
#6 /home/forge/www.my-website.com/vendor/statamic/cms/src/Auth/Eloquent/User.php(234): Statamic\Auth\Eloquent\User->permissions()
#7 /home/forge/www.my-website.com/vendor/statamic/cms/src/Auth/User.php(103): Statamic\Auth\Eloquent\User->hasPermission()

According to ChatGPT anyway. We are using Statmic but I setup my own roles thing, I guess they could be conflicting somehow.

We also just recently moved domains, and the Statamic pro license is showing missing. That could also be causing it. I will go look into that, but suggestions about the permissions conflict are welcome. I'm not 100% sure where to start with that. But I will report back later if I find anything out.

Please or to participate in this conversation.