2

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?

6 Answers 6

6

In my case it was because I deleted

'default' => 'mysql',

by mistake from app/config/database.php.

Sign up to request clarification or add additional context in comments.

Comments

3

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'    => '',
    ),

2 Comments

I did it but it does not work. I also updated laravel to 4.1 and still get the error.
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).
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

Comments

1

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

Comments

0

If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.

<?php 
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Character extends Model {

    protected $connection = 'my_db_connection_name_here';
}

This would be on the of the connections defined in config/database.php.

Comments

0

I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.