@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.
Oct 2, 2023
2
Level 3
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?
Please or to participate in this conversation.