【问题标题】:(Undefined index: driver) Why I am obtaining this error message when I try to perform a database query on a Laravel application?(未定义索引:驱动程序)为什么当我尝试在 Laravel 应用程序上执行数据库查询时收到此错误消息?
【发布时间】:2017-02-25 17:40:05
【问题描述】:

我是 Laravel 的新手,我对如何正确配置数据库连接有以下疑问。

我正在使用 Laravel 5.4,我需要连接到现有的 MySql 数据库。所以按照本教程:https://laravel.com/docs/5.4/database

我以这种方式设置了我的 config/database.php 文件:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        /*
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],
        */

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'pandaok'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        /*
        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],
        */

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    /*
    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],
    */

];

正如你在前面的代码中看到的 sn-p 我注释了所有除了与 MySql 相关的设置。

我也评论了这一行:

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

为了避免它从 .env 文件中获取连接参数,其中包含:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

但是这样做时,当我对类执行此查询时,我会收到此错误消息 $resut = DB::select('select * from pm_user where id = ?', [1]);:

ErrorException in DatabaseManager.php line 112:
Undefined index: driver
in DatabaseManager.php line 112
at HandleExceptions->handleError(8, 'Undefined index: driver', 'C:\\xampp\\htdocs\\HotelRegistration\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\DatabaseManager.php', 112, array('name' => null, 'config' => array('mysql' => array('driver' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null)))) in DatabaseManager.php line 112
at DatabaseManager->makeConnection(null) in DatabaseManager.php line 71
at DatabaseManager->connection() in DatabaseManager.php line 322
at DatabaseManager->__call('select', array('select * from pm_user where id = ?', array(1))) in Facade.php line 221
at DatabaseManager->select('select * from pm_user where id = ?', array(1)) in Facade.php line 221
..............................................................................
..............................................................................
..............................................................................

为什么?怎么了?我错过了什么?我该如何解决这个问题?

为什么 Laravel 在 2 个不同的地方(config/database.php 文件和 .env 文件)定义数据库连接参数?

【问题讨论】:

  • 以下答案之一是否解决了您的问题?

标签: php laravel laravel-5 laravel-5.4


【解决方案1】:

首先你需要取消注释这一行:

'default' =&gt; env('DB_CONNECTION', 'mysql'),

Laravel 没有在 2 个不同的地方定义它。如果引用 env 函数签名,则当 .env 文件中没有定义名为 DB_CONNECTION 的变量时,第二个参数是默认值

如果这仍然不起作用,请尝试php artisan config:clear,然后是php artisan config:cache。如果您需要任何进一步的帮助,请发布错误

【讨论】:

    【解决方案2】:

    您在这里遇到的错误:

    HandleExceptions->handleError(8, '未定义索引: 驱动',...

    是因为您已将'default' =&gt; env('DB_CONNECTION', 'mysql'), 注释掉。

    如果您不希望 .env 文件指示使用什么连接,那么就不要使用 env() 辅助函数,即

    'default' => 'mysql'
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      您应该注意几个层面。正如您可能知道的那样,永远不应将数据库凭据提交到您的代码存储库中,因此放置它们的自然位置是环境变量。

      因此,最好避免将它们直接放在 config/database.php 中。

      如您所述,在本地开发期间,它们应该放在您的 .env 文件中。但是,如果它们没有更新并且您收到此错误,则很可能您刚刚缓存了不正确的配置文件,您可以通过执行以下操作来清除它:

      php artisan config:clear
      

      虽然您可能还需要在此处删除注释掉的代码:

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

      然而,laravel 文档浏览的一个特性是 .env 文件不应该在生产环境中使用。 Laravel 使用了一个名为 phpdotenv 的包:

      您可以在此处阅读更多信息:https://github.com/vlucas/phpdotenv

      phpdotenv 是为开发环境而设计的,一般不应该在生产环境中使用。

      解决方法是继续使用 .env 来存储变量,但请确保它们都在 laravel 配置文件的 config/database.php 中引用并运行此命令laravel 不依赖生产环境中的 .env 文件

      php artisan config:cache
      

      【讨论】:

      • 这不是我的问题,罗斯 ;)
      • 哈哈,对不起。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 2015-10-09
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多