laravel database connection returns undefined index error

19,794

Solution 1

In my case it was because I deleted

'default' => 'mysql',

by mistake from app/config/database.php.

Solution 2

Moving the 'driver' key up a level should fix the issue.

$connections = array(
    'mysql' => array(
        'read' => array(
            'host'      => 'localhost',
            'database'  => 'app_system',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
        'write' => array(
            'host'      => 'localhost',
            'database'  => 'app_system',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
        'driver' => 'mysql'
    ),

Most of the other params that are shared can me moved as well

$connections = array(
    'mysql' => array(
        'read' => array(
            'host'      => 'localhost',
        ),
        'write' => array(
            'host'      => 'localhost',
        ),
        'driver'    => 'mysql',
        'database'  => 'app_system',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

Solution 3

This happens to me because I deleted

'default' =>env('DB_CONNECTION', 'mysql'),

From app/config/database.php. It's necesary have a default connection

Solution 4

Go to root directory .env This values are taken first.

Share:
19,794
Onur
Author by

Onur

Just another developer!

Updated on June 04, 2022

Comments

  • Onur
    Onur almost 2 years

    I am developing a project using laravel 4 framework. In my database.php file I get the following error:

      Undefined index: driver 
    

    And my connection is as following:

        $connections = array(
                'mysql' => array(
                    'read' => array(
                        'host'      => 'localhost',
                        'driver'    => 'mysql',
                        'database'  => 'app_system',
                        'username'  => 'root',
                        'password'  => 'root',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
                    ),
                    'write' => array(
                        'host'      => 'localhost',
                        'driver'    => 'mysql',
                        'database'  => 'app_system',
                        'username'  => 'root',
                        'password'  => 'root',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
                    ),
                ),
    
                'mysql2' => array(
                    'read' => array(
                        'host'  => 'localhost',
                        'driver'    => 'mysql',
                        'database'  => 'app_userdata',
                        'username'  => 'root',
                        'password'  => 'root',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',                      
                    ),
                    'write' => array(
                        'host'  => 'localhost',
                        'driver'    => 'mysql',
                        'database'  => 'app_userdata',
                        'username'  => 'root',
                        'password'  => 'root',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',                      
                    ),
                )
            );
    

    I am also using environments in order to set different mysql connections. What is wrong with the code?

  • Onur
    Onur over 10 years
    I did it but it does not work. I also updated laravel to 4.1 and still get the error.
  • EspadaV8
    EspadaV8 over 10 years
    Are you still getting the same error? I copy/pasted that code from a working L4.1 site that I've been working on (just changing the values). You will need to make the changes to the mysql2 section as well (although I would initially try with just 1 connection section for debugging).