(未定义索引:驱动程序)为什么我在 Laravel 应用程序上尝试执行数据库查询时收到此错误消息?
(Undefined index: driver) Why I am obtaining this error message when I try to perform a database query on a Laravel application?
我是 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,
],
],
*/
];
正如您在前面的代码片段中看到的,除了与 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
但是这样做时,当我对 class $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 在两个不同的地方定义数据库连接参数(config/database.php 文件和 .env 文件)?
首先你需要取消注释这一行:
'default' => env('DB_CONNECTION', 'mysql'),
Laravel 没有在两个不同的地方定义它。如果引用 env 函数签名,则第二个参数是 .env 文件中没有定义名为 DB_CONNECTION
的变量时的默认值
如果仍然无效,请尝试 php artisan config:clear
,然后是 php artisan config:cache
。 Post 如果您需要任何进一步的帮助,请查看错误信息
您在这里遇到的错误:
HandleExceptions->handleError(8, 'Undefined index: driver',...
是因为你注释掉了'default' => env('DB_CONNECTION', 'mysql'),
。
如果您不希望 .env
文件指定使用什么连接,那么就不要使用 env()
辅助函数,即
'default' => 'mysql'
希望对您有所帮助!
您应该了解几个级别。正如您可能知道的那样,数据库凭据永远不应该提交到您的代码存储库中,因此放置它们的自然位置是环境变量。
因此,最好避免将它们直接放在config/database。php。
在本地开发过程中,它们应该像您提到的那样放在您的 .env 文件中。但是,如果它们没有更新并且您收到此错误,则可能是您刚刚缓存了不正确的配置文件,您可以通过以下方式清除此错误:
php artisan config:clear
尽管您可能还需要删除此处注释掉的代码:
//'default' => env('DB_CONNECTION', 'mysql'),
但是,laravel 文档浏览过的一个特性是 .env 文件不应在生产中使用。 Laravel 使用名为 phpdotenv 的包:
您可以在此处阅读更多相关信息:https://github.com/vlucas/phpdotenv
phpdotenv is made for development environments, and generally should not be used in production.
一个解决方法是继续使用 .env 来存储您的变量,但要确保它们都在 config/database.php 在你的 laravel 配置文件和 运行 这个命令中,这样 laravel 就不会依赖生产中的 .env 文件
php artisan config:cache
我是 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,
],
],
*/
];
正如您在前面的代码片段中看到的,除了与 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
但是这样做时,当我对 class $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 在两个不同的地方定义数据库连接参数(config/database.php 文件和 .env 文件)?
首先你需要取消注释这一行:
'default' => env('DB_CONNECTION', 'mysql'),
Laravel 没有在两个不同的地方定义它。如果引用 env 函数签名,则第二个参数是 .env 文件中没有定义名为 DB_CONNECTION
的变量时的默认值
如果仍然无效,请尝试 php artisan config:clear
,然后是 php artisan config:cache
。 Post 如果您需要任何进一步的帮助,请查看错误信息
您在这里遇到的错误:
HandleExceptions->handleError(8, 'Undefined index: driver',...
是因为你注释掉了'default' => env('DB_CONNECTION', 'mysql'),
。
如果您不希望 .env
文件指定使用什么连接,那么就不要使用 env()
辅助函数,即
'default' => 'mysql'
希望对您有所帮助!
您应该了解几个级别。正如您可能知道的那样,数据库凭据永远不应该提交到您的代码存储库中,因此放置它们的自然位置是环境变量。
因此,最好避免将它们直接放在config/database。php。
在本地开发过程中,它们应该像您提到的那样放在您的 .env 文件中。但是,如果它们没有更新并且您收到此错误,则可能是您刚刚缓存了不正确的配置文件,您可以通过以下方式清除此错误:
php artisan config:clear
尽管您可能还需要删除此处注释掉的代码:
//'default' => env('DB_CONNECTION', 'mysql'),
但是,laravel 文档浏览过的一个特性是 .env 文件不应在生产中使用。 Laravel 使用名为 phpdotenv 的包:
您可以在此处阅读更多相关信息:https://github.com/vlucas/phpdotenv
phpdotenv is made for development environments, and generally should not be used in production.
一个解决方法是继续使用 .env 来存储您的变量,但要确保它们都在 config/database.php 在你的 laravel 配置文件和 运行 这个命令中,这样 laravel 就不会依赖生产中的 .env 文件
php artisan config:cache