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

jordan.dobrev.88@gmail.com's avatar

Issue with parallel requests

Hi all,

I have set up Laravel5 with XAMPP on windows and I have an application that required multiple php ajax calls to be executed at a time. The application uses 2 database connections. Sometimes I get the following error:

Access denied for user ''@'localhost' to database 'forge'

My .evn file has totally different MySql configuration so it seems Laravel cannot get the ENV configuration properly and sets the settings to their defaults.

My env is the right one:

php artisan env
Current application environment: local

Here is my stack trace:

[2015-04-01 10:48:32] local.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'forge'' in C:\app\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47
Stack trace:
#0 C:\app\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(47): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
#1 C:\app\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php(20): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=loca...', Array, Array)
#2 C:\app\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(58): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 C:\app\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php(47): Illuminate\Database\Connectors\ConnectionFactory->createSingleConnection(Array)
#4 C:\app\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(177): Illuminate\Database\Connectors\ConnectionFactory->make(Array, 'gateway')
#5 C:\app\vendor\laravel\framework\src\Illuminate\Database\DatabaseManager.php(65): Illuminate\Database\DatabaseManager->makeConnection('gateway')
#6 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3132): Illuminate\Database\DatabaseManager->connection(NULL)
#7 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3098): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#8 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1891): Illuminate\Database\Eloquent\Model->getConnection()
#9 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1832): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#10 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1806): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#11 C:\app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(3319): Illuminate\Database\Eloquent\Model->newQuery()
#12 C:\app\app\Repositories\RepositoryEloquent.php(144): Illuminate\Database\Eloquent\Model->__call('findOrFail', Array)
#13 C:\app\app\Repositories\RepositoryEloquent.php(144): App\Model\Organization->findOrFail('1', Array)
#14 C:\app\app\Providers\RouteServiceProvider.php(38): App\Repositories\RepositoryEloquent->getByIdOrFail('1')
#15 [internal function]: App\Providers\RouteServiceProvider->App\Providers\{closure}('1', Object(Illuminate\Routing\Route))
.............
.............
.............

My Route binding is

public function boot(Router $router)
{
    $router->bind('organization', function ($organization) {
            return $this->app->make(OrganizationRepository::class)->getByIdOrFail($organization);
    });  
}   
0 likes
6 replies
Michael__'s avatar

Was about to ask that as well. Having the exact same issue. To reproduce this, you can simply quickly hit F5. It seems it does not correctly load the values form the .env files if you do that.

jordan.dobrev.88@gmail.com's avatar

If we use ENV vars instead of the .env file it would probably work. We might do it by setting them globally or via the apache SetEnv command in the vhost file(or the nginx alternative). Still I think there should be a fix for that issue.

jordan.dobrev.88@gmail.com's avatar

Same thing happens when you have 2 laravel projects on the same OS. They share the same env vars and if both scripts execute at the same time they crash. Seems like using env vars like this is a bad idea after all and the old l4 way worked way better.

2 likes

Please or to participate in this conversation.