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

projectmentor's avatar

How to determine current database driver

Hi guys,

What is the best way to determine the current database driver?

Currently using this code in Laravel 5.2:

switch(DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME))
{
    case 'mysql':
        
        //mysql syntax
        break;

    case 'sqlite':
        
        //sqlite syntax
        break;

    default:
        throw new \Exception('Driver not supported.);
        break;
}

In this case I can't just use Eloquent (unfortunately), so I need to be able to dynamically change syntax for a complex sql query that is not portable between different drivers. i.e: upsert

I just need a quick and dirty way to substitute the sql syntax based on which driver currently connected.

1 like
6 replies
alfonsobries's avatar

I think the config helper should do the trick:

config('mail.driver'); // should returns for example 'mysql'
2 likes
Danatela's avatar

Generally, mail driver is veeeery different from database driver. In my particular case, it is smtp.

This should not work.

2 likes
jbloomstrom's avatar
Level 50

$connection = config('database.default');

$driver = config("database.connections.{$connection}.driver");

11 likes
chargoy's avatar

You can use this too:

use Illuminate\Support\Facades\DB;

DB::getConfig("driver")
1 like
damienl's avatar

It's an old conversation but i think a cleaner way today to get current database drive would be to check (DB::connection())::class :

if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) {
    // ...
}
1 like

Please or to participate in this conversation.