Laravel:未定义索引:驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46066886/
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 16:36:32  来源:igfitidea点击:

Laravel : Undefined index: driver

phpmysqllaravel

提问by zwl1619

I am using Laravel 5.5 and I need to change database dynamically,
For example, there are two databases,db1and db2,there is a table articlesin each database.

我正在使用 Laravel 5.5,我需要动态更改数据库,
例如,有两个数据库,db1并且每个数据库中db2有一个表articles

Now I want to copy articles from db1to db2,

现在,我想从文章复制db1db2

in .envfile, the current database is db1:

.env文件中,当前数据库是db1

DB_DATABASE=db1

I want to change it dynamically when copying records, I tried to do it like this:

我想在复制记录时动态更改它,我尝试这样做:

public function test()
{
    $articles=Article::all();

    Config::set("database.connections.mysql", [
        "host" => "127.0.0.1",
        "database" => "db2",
        "username" => "root",
        "password" => ""
    ]);
    //DB::purge('mysql');  //this line exists or not,it has the same error.
    DB::table('articles')->insert($articles);
    dd('ok');
}

but there is an error:

但有一个错误:

Undefined index: driver

未定义索引:驱动程序

I have many databases, so I don't want to change it in .envfile.
What should I do?

我有很多数据库,所以我不想在.env文件中更改它。
我该怎么办?

update:

更新:

in config/database.phpit has the two items:

config/database.php它有两个项目:

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

'connections' => [

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

],

and I try

我尝试

    Config::set("database.connections.mysql", [
       'mysql' => [
           "host" => "127.0.0.1",
           "database" => "db2",
           "username" => "root",
           "password" => ""
        ]
    ]);

The error still exists.

错误仍然存​​在。

回答by chanafdo

The error is because you are missing the driverin your configuration.

错误是因为您driver的配置中缺少。

A better way of changing the connection would be registering your new connection in database configuration file and change the connection at runtime.

更改连接的更好方法是在数据库配置文件中注册新连接并在运行时更改连接。

'connections' => [

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

    'newConnection' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => 'db2',
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
]

Now you can change your connection using the name you used to define your new connection.

现在您可以使用用于定义新连接的名称来更改您的连接。

Change the default connection

更改默认连接

Config::set('database.default', 'newConnection');
DB::reconnect('newConnection');

or change the connection for a query builder

或更改查询构建器的连接

DB::connection('newConnection')->table('articles')->insert($articles);

or if you are using Eloquent models you can set the default connection associated with the model using the connectionproperty

或者,如果您使用的是 Eloquent 模型,则可以使用connection属性设置与模型关联的默认连接

protected $connection = 'newConnection';

or change at runtime by calling setConnection

或通过调用在运行时更改 setConnection

(new User)->setConnection('newConnection');

If you wish to change the current connection details you can change them as you wish

如果您想更改当前的连接详细信息,您可以随意更改它们

Config::set('database.connections.mysql.database', 'db2');

and after changes you need to call

更改后,您需要致电

DB::reconnect('mysql');

or

或者

DB::purge('mysql');

回答by Exprator

public function test()
{
    $articles=Article::all();

    Config::set("database.connections.mysql", [
        "driver" => "mysql"
        "host" => "127.0.0.1",
        "database" => "db2",
        "username" => "root",
        "password" => ""
    ]);
    //DB::purge('mysql');  //this line exists or not,it has the same error.
    DB::table('articles')->insert($articles);
    dd('ok');
}

you were missing the driver , as laravel needs what driver of database you want to use, as you are using database.connection.mysql it will set the values in that array, but still the driver is needed

您缺少驱动程序,因为laravel 需要您要使用的数据库驱动程序,因为您使用的是database.connection.mysql 它将设置该数组中的值,但仍然需要驱动程序

as for example 


'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),

        ],

回答by N. Djokic

In my case driver was configured as well as everything else but it threw that error when I hit any endpoint. I was not able to delete cache with php artisan command so for me solution was to manually delete cache files from app/bootstrap/cache folder as old configuration stayed there and did not recognized new database connection.

在我的情况下,驱动程序与其他所有内容一样被配置,但是当我点击任何端点时它会抛出该错误。我无法使用 php artisan 命令删除缓存,所以对我来说,解决方案是手动从 app/bootstrap/cache 文件夹中删除缓存文件,因为旧配置留在那里并且无法识别新的数据库连接。