Laravel:未定义索引:驱动程序 [英] Laravel : Undefined index: driver

查看:39
本文介绍了Laravel:未定义索引:驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在我想将文章从 db1 复制到 db2

Now I want to copy articles from db1 to db2,

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

in .env file, the current database is 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');
}

但是有一个错误:

未定义索引:驱动程序

Undefined index: driver

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

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

更新:

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,
    ],

],

我尝试

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

错误仍然存​​在.

推荐答案

该错误是因为您在配置中缺少 driver .

The error is because you are missing the driver in your configuration.

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

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.

更改默认连接

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

或更改查询构建器的连接

or change the connection for a query builder

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

或者如果您使用的是雄辩模型,则可以使用 connection 属性

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

protected $connection = 'newConnection';

或在运行时通过调用 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');

更改后,您需要致电

DB::reconnect('mysql');

DB::purge('mysql');

这篇关于Laravel:未定义索引:驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆