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

cutups's avatar

Running any `php artisan` command tells me Table `database.permissions` doesn't exist

Running any php artisan command tells me Table 'database.permissions' doesn't exist

If I run this commend with my normal normal testing database set up - which includes the permissions table - any artisan command works fine.

But as part of a CI test script, I need to run some artisan commands before the database is migrated, and found I'm getting this error.

I'm using Laravel 8 (only recently upgraded the same project from 6 to 7 and now to 8).

Any suggestions on debugging this?

0 likes
4 replies
cutups's avatar

After a little more digging, turns out it was some code in my AuthServiceProvider boot method:

    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Gate::before(function ($user) {
            if ($user->hasGroup('admin')) {
                return true;
            }
        });

        // adds a gate for each permission name - checks whether the user has a group that matches one of the permission groups
        // disabling because this is causing an issue with php artisan when the database isn't initialized
        foreach ($this->getPermissions() as $permission) {
            Gate::define($permission->name, function ($user) use ($permission) {
                return $user->hasGroup($permission->groups);
            });
        };

        Gate::before(function ($user) {
            if ($user->hasGroup('admin')) {
                return true;
            }
        });
    }

It's trying to query for permissions when there's no tables created yet.

Anyone have a good strategy for handling this?

Snapey's avatar

You could exit the provider if running in console?

    if(app()->runningInConsole()) {
        return;
    }
cutups's avatar

Ended up just checking if the schema has that table at the time before querying.

        if (!Schema::hasTable('permissions')) {
            return [];
        }
Snapey's avatar

bear in mind that you have added a DB request to every request to your application.

1 like

Please or to participate in this conversation.