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

Svetlio's avatar

Problem after upgrading to php 8.1 and Laravel 9

I started upgrading my whole project from php 7.4 and laravel 8 to php 8.1 and Laravel 9. I went through the tutorials and everything the upgrade went well but I have an interesting problem after the upgrade. The application gives the ability the users to create their own "restaurants" where they can store recipes. Normal users can only access their own restaurants but as an admin you can go to whichever restaurant you want. The problem is the following: I am not able to go and view the restaurant unless I am part of it(you are able to add guest users to your restaurant). Before the update everything was completely fine but now when I try and go into the restaurant I get the following error:

  • exception: "Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException" file: "C:\xampp\htdocs\CalcEasy\CalcuEasy-2\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php" line: 382 message: "This action is unauthorized." *

I have a few policies like :

public function view(User $user)
    {
        return $user->getAbilities()
            ->where('restaurant_id', app('restaurant')->id)
            ->whereIn('name', ['edit restaurant', 'view restaurant'])->isNotEmpty();
    }

The normal laravel stuff. We have roles for the users which we assign through Bouncer and then we have admin role which previously worked completely fine before the upgrade. The error I am thrown is not very helpful at all but in the stacktrace I see that the only file I have control over is app\Http\Middleware\SetLocale.php The error there is thrown on line 27 return $next($request);

public function handle($request, Closure $next)
    {
        if (Auth::user() != null) {
            app()->setLocale(Auth::user()->language->abbreviation);
        }

        return $next($request);
    }

When I try to log the same line app()->setLocale(Auth::user()->language->abbreviation); on my other machine which is still on php 7 and laravel 8 it gives me the same thing as it does now(after the upgrade) and it is making me even more confused.

The dependencies that I have upgraded during all this are:

"spatie/laravel-ignition": "^1.0",
 "barryvdh/laravel-snappy": "^1.0",
"codedredd/laravel-soap": "^3.0",
 "kalnoy/nestedset": "^6.0",
"laravel/framework": "^9.0",
 "league/flysystem-aws-s3-v3": "^3.0",
 "psr/cache": "^3.0",
"psr/link": "^2.0",
 "silber/bouncer": "^1.0",
"spatie/laravel-permission": "^5.0",
 "symfony/symfony": "^6.1",
 "beyondcode/laravel-dump-server": "1.8.0",
"filp/whoops": "^2",
 "fzaninotto/faker": "1.9.x-dev",
"nunomaduro/collision": "^6.1",
0 likes
2 replies
Sinnbeck's avatar

Where do you give access to the admin? I assume something like this?

        Gate::before(function ($user, $ability) {
            return $user->hasRole('Admin') ? true : null;
        });
Svetlio's avatar

@Sinnbeck Admins by default have all of our roles and when you want to access the restaurant on the index method we have this

if (Auth::user()->can('view', $restaurant)) {
		//load all of the stuff in here 
}

Interesting enough it gives me that can() Method 'can' not found in \Illuminate\Contracts\Auth\Authenticatable|null and on my php 7.4 machine works fine and transfers me to a class called Authorizable.php in the facade namespace Previously it worked fine but I don't know what broke it now

Please or to participate in this conversation.