harishkumar's avatar

Laravel gate error, no such table: permissions (SQL: select * from “permissions”)

For laravel testing environment, Laravel gate is not working. In phpunit.xml file, I am using sqlite connection and :memory: as database.

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

In AuthServiceProvider, I am defining gate as in this code below.

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);


    foreach($this->getPermissions() as $permission) {
        $gate->define($permission->name, function($user) {
            $user->hasRole($permission->roles);
        });
    }
}

protected function getPermissions() {
    return Permission::with('roles')->get();
}

So, when ever I run phpunit. It shows error no such table: permissions (SQL: select * from "permissions").

So, please guide how can I define gate after migration for testing environment.

0 likes
2 replies
Thyrosis's avatar

It seems like you're missing the migration file for you permissions table. Did you create the table manually or us a migration?

In case of the latter, check the spelling. Maybe you changed its name after you ran the migration? And are you including the migrateDatabase trait in your test file?

Also, does it work in 'the real world', so when you browse to your app? In that case, the table does exist, but doesn't get created by phpunit (hence the migration question).

harishkumar's avatar

@Thyrosis Yes, I have created migration. And it is working perfectly on local environment. I have issue only in testing environment, when I run phpunit commend on terminal.

Please or to participate in this conversation.