简体   繁体   English

Laravel - 捕获 php 工匠命令

[英]Laravel - Catch php artisan commands

I've did some changes in my config/app to use multiple databases selected by front-end, now I have to tell in \Request()->header('database') which database I want access.我在config/app中进行了一些更改以使用前端选择的多个数据库,现在我必须在\Request()->header('database')中告诉我要访问哪个数据库。

It's work perfectly, the problem is: when I try to do any artisan commands my logic dies, because isn't informed the database.它工作得很好,问题是:当我尝试执行任何工匠命令时,我的逻辑死了,因为没有通知数据库。

So I need to inform the database in artisan commands , like that:所以我需要在artisan commands中通知数据库,如下所示:

php artisan migrate --database=sandiego_school
php artisan migrate:rollback --database=newyork_school 

How can I observer all commands to get the argument?我如何观察所有命令以获取参数?

In this case I guess you should create your own commands that overrides the commands you want to call, then in the handle method of the command you could specify the connexion you want to work on:在这种情况下,我想您应该创建自己的命令来覆盖您要调用的命令,然后在命令的句柄方法中,您可以指定要处理的连接:

\DB::setDefaultConnection($connexion);

or also you can simply add header to request:或者您也可以简单地添加 header 来请求:

request()->headers->set('database', $dbname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM