laravel 数据库连接返回未定义索引错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21228148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:53:37  来源:igfitidea点击:

laravel database connection returns undefined index error

phpmysqldatabaselaravellaravel-4

提问by Onur G?ker

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

我正在使用 laravel 4 框架开发一个项目。在我的 database.php 文件中,我收到以下错误:

  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?

我也在使用环境来设置不同的 mysql 连接。代码有什么问题?

回答by ubuntus

In my case it was because I deleted

就我而言,这是因为我删除了

'default' => 'mysql',

by mistake from app/config/database.php.

错误地来自 app/config/database.php。

回答by EspadaV8

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

回答by user3818251

This happens to me because I deleted

这发生在我身上,因为我删除了

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

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

来自 app/config/database.php。必须有默认连接

回答by a3rxander

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

转到根目录 .env 这个值首先被采用。

回答by The Unknown Dev

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.

如果您有多个不同的连接(例如到同一主机上的多个数据库)并且设置默认连接没有意义,您可以在 Model 类中指定一个连接。

<?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.

这将在 中定义的连接上config/database.php

回答by veyselsahin

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

我通过添加一些权限解决了我的问题。我的 .env 文件存在但不可读。我刚刚添加了 755 权限。