Laravel动态数据库 [英] Laravel Dynamic Database's

查看:116
本文介绍了Laravel动态数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel Config :: set Persist Through Requests?

在得到以下答案后,我尝试了...

After getting the answer below, I tried it out...

'default' => 'mysql_main',
    'connections' => [
        'mysql_main' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
        'mysql_company' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => Auth::user()->club->db_name,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

    ],

但是,在配置下的database.php文件夹中执行此操作时,我收到以下错误...

However, upon doing this inside the database.php folder under config I receive the following error...

致命错误:在第73行的F:\trapstats_v5\config\database.php中找不到类'Auth'。

是否有另一种方式根据用户进行动态数据库连接,这将通过请求而不是执行<$ c $每次我要访问动态连接时,都需要配置([database.connections.mysql_company.database'=> Auth :: user() - > club-> db_name])

Is there another way to do dynamic database connections, based on the user, that will save through requests instead of doing config([database.connections.mysql_company.database' => Auth::user()->club->db_name]) every time I want to access the dynamic connection?

此问题类似于动态数据库连接的答案在拉里维尔如果我也这样做,我会得到同样的错误,除了这个时候它被称为Session而不是Auth。

This question is similar to the answer of Dynamic database connection in Laravel. And if I do this answer as well I get the same sort of error except this time it is called Session instead of Auth.

推荐答案

做了一些更多的阅读和回答,并提出了很多问题,我提出了一个解决方案。

After doing some more reading and going around and asking many questions I have come up with a solution.

我最后做的是创建一个名为数据库的中间件,运行后每个其他中间件完成。这允许使用所有典型的Laravel服务(例如Auth :: user());

What I ended up doing was creating a middleware called Database that ran AFTER every other middleware finished. This allowed for use of all of the typical Laravel services (like Auth::user());

数据库

class数据库
{
/ **
*处理传入的请求。
*
* @param \Illuminate\Http\Request $ request
* @param \Closure $ next
* @return mixed
* /
public function handle($ request,Closure $ next)
{
if(!Auth :: guest()){
config(['database.connections.club.database' => Auth :: user() - > club-> db_name]);
}
return $ next($ request);
}
}

然后对于路由组,我分配这个中间件。

Database class Database { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!Auth::guest()) { config(['database.connections.club.database' => Auth::user()->club->db_name]); } return $next($request); } } And then for the route group I assign this middleware to it.

这篇关于Laravel动态数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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