Alter base PDO configuration in Laravel

2019-05-06 17:06发布

问题:

My shared web host have some problems with query prepares and I want to enable PDO's emulated prepares, there's no option for this in the config\database.php.

Is there any way I can do that in Laravel?

回答1:

You can add an "options" array to add options to your Database Connection within config/database.php:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
        // Additional options here
        'options'   => [PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_COMPRESS => true,]
    ],

You will see I've also switched on MYSQL_ATTR_COMPRESS for my connection.

You can find more information on some of the options they have built in here:

https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connectors/Connector.php