我想使用控制台命令
迁移数据库当我尝试php artisan migrate
正在运行时,但当我尝试php artisan migrate --database="test"
无法正常工作时说
[InvalidArgumentException]
Database [test] not configured.
出了什么问题?这是php artisan migrate --help
Usage:
migrate [options]
Options:
--database[=DATABASE] The database connection to use.
--force Force the operation to run when in production.
--path[=PATH] The path of migrations files to be executed.
--pretend Dump the SQL queries that would be run.
--seed Indicates if the seed task should be re-run.
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under.
更新
在我意识到它不是数据库名称,而是数据库连接..但我需要调用数据库名称,因为我从另一个项目调用它来创建新数据库并迁移它。所以我需要动态..如何实现?
答案 0 :(得分:0)
你检查过config / database.php并查看'connections'数组吗?尝试这样的事情(在我使用mysql的例子中):
'connections' => [
// Replace mysql with your DB driver
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
在.env文件中,不应设置任何DB_参数。如果您使用上述设置运行php artisan migrate,它将迁移到homestead数据库。如果你运行php artisan migrate --database = test,它将迁移到测试数据库。
答案 1 :(得分:0)
我找到了一些完全符合我需要的包裹。
https://packagist.org/packages/a2way/laravel-tenant-migrate
我可以为每个租户运行migrate:tenant (connection-name) (database-name)
for create database ..
答案 2 :(得分:0)
env('DB_DATABASE', 'test')
这就是原因。
读取 .env 文件中的设置。
我设置了⇃。
'database' => 'test',