Laravel动态数据库 [英] Laravel Dynamic Database's
问题描述
( Laravel Config :: set Persist Through Requests?)
获得以下答案后,我尝试了...
'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文件夹内,我收到以下错误...
致命错误:在第73行上的F:\trapstats_v5\config\database.php中找不到类'Auth'。
有另一种方法来基于用户执行动态数据库连接,这将通过请求而不是 config([database.connections.mysql_company.database'=> Auth :: user() - > club-> db_name])
每次我想访问动态连接?
此问题类似于动态数据库连接的答案在Laravel 。如果我做这个答案,我得到相同的错误,除非这次被称为Session而不是Auth。
在做了一些更多的阅读和回答,并提出了很多问题,我想出了一个解决方案。
我最后做的是创建一个名为Database的中间件,运行AFTER每隔一个中间件完成。这允许使用所有典型的Laravel服务(如Auth :: user());
数据库
类数据库
{
/ **
*处理传入请求。
*
* @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);
}
}
然后对于路由组,我将该中间件分配给它。
(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,
],
],
However, upon doing this inside the database.php folder under config I receive the following error...
Fatal error: Class 'Auth' not found in F:\trapstats_v5\config\database.php on line 73.
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?
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.
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());
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屋!