laravel/framework · error · InvalidArgumentException

A driver must be specified.

Error message

A driver must be specified.

What it means

Thrown by ConnectionFactory::createConnector() when the 'driver' key is missing from the connection config array. Without a driver the factory cannot pick a Connector (MySQL/Postgres/SQLite/etc.) or a connection resolver.

Source

Thrown at src/Illuminate/Database/Connectors/ConnectionFactory.php:273

     * @return \Closure
     */
    protected function createPdoResolverWithoutHosts(array $config)
    {
        return fn () => $this->createConnector($config)->connect($config);
    }

    /**
     * Create a connector instance based on the configuration.
     *
     * @param  array  $config
     * @return \Illuminate\Database\Connectors\ConnectorInterface
     *
     * @throws \InvalidArgumentException
     */
    public function createConnector(array $config)
    {
        if (! isset($config['driver'])) {
            throw new InvalidArgumentException('A driver must be specified.');
        }

        if ($this->container->bound($key = "db.connector.{$config['driver']}")) {
            return $this->container->make($key);
        }

        return match ($config['driver']) {
            'mysql' => new MySqlConnector,
            'mariadb' => new MariaDbConnector,
            'pgsql' => new PostgresConnector,
            'sqlite' => new SQLiteConnector,
            'sqlsrv' => new SqlServerConnector,
            default => throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]."),
        };
    }

    /**
     * Create a new connection instance.

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Set DB_CONNECTION in .env (e.g. mysql, pgsql, sqlite, sqlsrv).
  2. Ensure every entry in config/database.php 'connections' has a 'driver' key.
  3. Clear config cache: php artisan config:clear then config:cache after fixing.
  4. Dump config to verify: php artisan tinker -> config('database.connections.mysql.driver').

Example fix

// before (.env)
DB_CONNECTION=

// after
DB_CONNECTION=mysql
Defensive patterns

Strategy: validation

Validate before calling

foreach (config('database.connections') as $name => $cfg) {
    if (! isset($cfg['driver'])) {
        throw new \RuntimeException("Connection [$name] is missing a 'driver' key.");
    }
}

Type guard

function hasDriver(array $config): bool {
    return isset($config['driver']) && is_string($config['driver']) && $config['driver'] !== '';
}

Prevention

When it happens

Trigger: A connection config array passed to the factory without a 'driver' key. Common when DB_CONNECTION is unset in .env, when a custom connection was added to database.connections without a driver, or when config gets cached stale.

Common situations: Fresh checkout where .env is missing or DB_CONNECTION is blank; misnamed connection key; config:cache run with an incomplete .env; overriding config in tests and dropping the driver key.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/815f57a2bb21c094.json. Report an issue: GitHub.