4

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?

Positivity
  • 5,406
  • 6
  • 41
  • 61

1 Answers1

8

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

Justin Origin Broadband
  • 1,492
  • 1
  • 11
  • 14
  • Thanks, I love looking at the codes behind open source things like laravel, how did you locate the corresponding file? – Positivity Jun 06 '16 at 13:06
  • 1
    To save space I created a Gist for you and others who may be interested. https://gist.github.com/justinobb/25995796ed1a8aa157b578c999544aa2 . In the case of your question, I searched for the common DB connector class, "Connector", under Illuminate/Database/Connectors which I searched for through the "API Documentation" pages. Glad I could help. Enjoy your Source Code browsing! – Justin Origin Broadband Jun 06 '16 at 14:06
  • 3
    Be aware that this setting ATTR_EMULATE_PREPARES => true, will stringify your database outputs. For example, a tinyint will outcome as an string – buzkall Oct 31 '17 at 09:39