Onur
Onur

Reputation: 451

laravel database connection returns undefined index error

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?

Upvotes: 2

Views: 21693

Answers (6)

veyselsahin
veyselsahin

Reputation: 388

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

Upvotes: 0

The Unknown Dev
The Unknown Dev

Reputation: 3118

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.

Upvotes: 0

Noe Lopez
Noe Lopez

Reputation: 305

This happens to me because I deleted

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

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

Upvotes: 3

ubuntus
ubuntus

Reputation: 193

In my case it was because I deleted

'default' => 'mysql',

by mistake from app/config/database.php.

Upvotes: 6

a3rxander
a3rxander

Reputation: 906

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

Upvotes: 1

EspadaV8
EspadaV8

Reputation: 668

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

Upvotes: 3

Related Questions