laravel database connection returns undefined index error

别等时光非礼了梦想. 提交于 2019-12-24 05:43:13

问题


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?


回答1:


In my case it was because I deleted

'default' => 'mysql',

by mistake from app/config/database.php.




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



回答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




回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/21228148/laravel-database-connection-returns-undefined-index-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!