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

mallorca's avatar

SQLSTATE error following ACL - Permissions and Roles

Hi artisans,

I followed the Roles and Permissions series on ACL: https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16

Everything seems to work fine, except I can't use the php artisan command after finishing the series. I get the following error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory

If I remove the foreach construct in AuthServiceProvider:

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

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

Then it works fine again. Does someone know what the problem is and how to fix it? I think that several others experienced the same problem in the comments but I couldn't find a good solution to it.

0 likes
7 replies
umanda's avatar

Open your /app/config/database.php and change "host" from "localhost" to "127.0.0.1". If you use .evn file please update that file

ie

if /app/config/database.php

'mysql' => [
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'database'  => 'your_database_name',
        'username'  =>  'your_database_user',
        'password'  => 'your_database_password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
     ],

if .env file

DB_HOST=127.0.0.1
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
mallorca's avatar

@umanda Thanks for the reply! I did that but now I get this instead:

[PDOException]
  SQLSTATE[HY000] [2002] Connection refused

I don't think thats the problem..

bashy's avatar

What are you using to host your MySQL server? Homestead?

If it is, you can use 192.168.10.10 as the db_host.

mallorca's avatar

@bashy Thanks! Yeah homestead, after changing it to 192.168.10.10 then it works again. Why does it this happen? I didn't experience this problem before adding the new ACL. I can see in the comments that others are experiencing problems doing some artisan commands like DB seeding.

bashy's avatar
bashy
Best Answer
Level 65

If you do the command from your local machine, it can't connect to the Homestead VM via localhost or 127.0.0.1 unless you use the forwarded port. It's not a bug but just a slight configuration error on your part.

Always remind yourself where the IP is and how it will connect.

mallorca's avatar

@bashy Thanks for the info, but the thing is it works fine when I don't have foreach construct in the boot method, and it has always worked using that config except after following the ACL series. I also noticed that other people have a lot of comments regarding problems with DB seed.. Maybe it's related?

bashy's avatar

Depends if certain commands use the DB connection. Has nothing to do with a foreach itself. Depends what data.

Please or to participate in this conversation.