laravel 更改数据库连接运行时

laravel Change Database connection run time(laravel 更改数据库连接运行时)
本文介绍了laravel 更改数据库连接运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要在运行时更改 Laravel 5 数据库连接.

I need to change laravel 5 database connection in run time.

你有什么想法吗?

请分享给我.

谢谢.

推荐答案

更新:这个答案来自 2015 年!我已经很多年没有使用 Laravel 了,我也没有掌握最新的最佳实践.这个答案不断得到支持,所以我认为它有效,但请谨慎行事.

Update: This answer is from 2015! I haven't used Laravel in years and I am not up to date with the latest best practices. This answer keeps getting upvoted so I suppose it works but please proceed with caution.

好吧,我对此的直接回答是:不要.您可以通过更改数据模型和使用一些更高级的关系来完成任务.不知道你想做什么很难说,但在我看来,这通常是个坏主意,特别是如果你打算使用 eloquent 模型等等

Well, my immediate answer to this is: don't. Chances are that you can accomplish your task by changing your data model and working with some more advanced relationships. It's hard to tell without knowing what you want to do but it seems to me like a bad idea in general, specially if you're planning on using eloquent models and so on

也就是说,在某些情况下,您确实需要更改另一个数据库中的数据或执行一些原始查询,您可以使用 DB::connection() 方法.类似的东西:

That said, in some scenario where you really need to alter data in another database or execute some raw query you can use the DB::connection() method. Something like:

$data = DB::connection('another_connection')->select(...);

您可以在 database.php 文件中指定 another_connection 变量.像这样:

You can specify that another_connection variable at your database.php file. Like this:

<?php
return array(

'default' => 'mysql',

'connections' => array(

    # Your regular connection
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'user',
        'password'  => 'password'
        'charset'   => 'utf8',

    ),

    # Your new connection
    'another_connection' => array(
        'driver'    => 'mysql',
        'host'      => 'another_host',
        'database'  => 'another_db',
        'username'  => 'user1',
        'password'  => 'password1'
        'charset'   => 'utf8',
    ),
),
);

您甚至可以使用 protected $connection = 'another_connection'; 为每个 eloquent 模型指定一个连接 也可以为每个在运行时创建/查询的模型实例指定一个连接

You can even specify a connection for each eloquent model using protected $connection = 'another_connection'; also you can specify a connection for each model instance created/queried at run time

$user = new User;
$user->setConnection('another_connection');
$user1 = $user->find(1);

但话说回来,我个人认为这不是一个好主意,而且在我看来,随着应用程序复杂性的增加,一切都会变得混乱并很快崩溃.

But then again, I personally don't think this is a good idea and it seems to me that everything can get messy and fall apart very quickly as your application grows in complexity.

这篇关于laravel 更改数据库连接运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)