久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 更改數據庫連接運行時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 欧美一区二区免费在线 | 中文字幕高清 | 亚洲精品无 | 日韩精品在线免费观看 | 欧美日韩亚洲系列 | 国产精品综合网 | 精品一区二区三区免费视频 | 精品av久久久久电影 | 精品一区二区av | 亚洲网站在线观看 | 欧美一区二区在线观看 | 免费的av网站 | 日韩性在线| 日韩精品一区在线 | 国产美女一区 | 精精国产xxxx视频在线播放7 | 亚洲久在线 | 91大片| 日韩久久久久 | 欧美日韩国产一区二区三区 | 性一爱一乱一交一视频 | 福利精品 | 欧美老少妇一级特黄一片 | 小视频你懂得 | 亚洲经典一区 | 天天操天天射天天舔 | 黄色大片观看 | 亚洲一区中文字幕 | 国产精品一区一区三区 | 精品国产精品一区二区夜夜嗨 | 一色桃子av一区二区 | 久久久久久久一区 | 日本久久网| 在线播放国产一区二区三区 | 成人在线视频免费观看 | 人人亚洲 | 黄色一级免费看 | 婷婷综合网| 欧美激情一区二区 | 亚洲一区二区三区免费 | 国产婷婷精品 |