在 Laravel/Homestead 中更改数据库名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25317370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing database name in Laravel/Homestead
提问by Kapil Sharma
I started learning Laravel just an hour ago and following tutorials.
一个小时前,我开始学习 Laravel 并遵循教程。
My settings for database are as follow (File: app/config/database.php):
我的数据库设置如下(文件:app/config/database.php):
'default' => 'mysql',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'laraveltest',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In mySQL on homestead, I already created laraveltest
database. I also created migration file in database/migration
directory with following code:
在 homestead 上的 mySQL 中,我已经创建了laraveltest
数据库。我还在database/migration
目录中使用以下代码创建了迁移文件:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
and migration commands were:
和迁移命令是:
vagrant@homestead:~/Code/Laravel$ php artisan migrate
Migration table created successfully.
Migrated: 2014_08_14_195103_create_users_table
As displayed, table users created but in homestead
database, not in laraveltest
database. Can someone please tell where I went wrong and how to force laravel to use laraveltest
database?
如图所示,表用户创建但在homestead
数据库中,而不是在laraveltest
数据库中。有人可以告诉我哪里出错了以及如何强制 laravel 使用laraveltest
数据库吗?
Edit after first commentFile: bootstrap/start.php
第一次评论后编辑文件:bootstrap/start.php
$env = $app->detectEnvironment(array(
'local' => array('homestead'),
));
回答by camroncade
It is most likely an issue with environments. Check that you have a database configuration file in app/config/local/, and check that it has the proper settings.
这很可能是环境问题。检查 app/config/local/ 中是否有数据库配置文件,并检查其设置是否正确。
回答by Karin van den Berg
If anyone finds this looking for the answer for Laravel 5 or later: It's the same as @camroncade says, but in this case it's your .env
file in the project root you want to look at.
如果有人在寻找 Laravel 5 或更高版本的答案时发现此问题:它与@camroncade 所说的相同,但在这种情况下,它是您.env
要查看的项目根目录中的文件。
回答by Andre F.
I am currently dealing with this issue as well. Changed the .env file numerous times as well as the config.php file. Nothing seemed to work. After having done some deep digging into the framework I have found an acceptable temporary workaround to the problem that does not conflict with ANY of Laravel 5.0.2's Exceptions.
我目前也在处理这个问题。多次更改 .env 文件以及 config.php 文件。似乎没有任何效果。在对框架进行了一些深入研究之后,我找到了一个可接受的临时解决方法,该解决方法与任何 Laravel 5.0.2 的异常都不冲突。
NOTE:I'm currently using Ampps 3.4 with php 7.0.2 phpmyadmin 4.4.15.1 on a Windows 8.1 OS.
注意:我目前在 Windows 8.1 操作系统上使用 Ampps 3.4 和 php 7.0.2 phpmyadmin 4.4.15.1。
Inside of the file "ConnectionFactory.php" (located at "laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors"),
scroll down to the public function make
located at line 41.
Within this function you can do the following afterthis statement $config = $this->parseConfig($config, $name);
which should be on line 43:
在文件“ ConnectionFactory.php”(位于“laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors”)中,向下滚动到public function make
位于第 41 行的位置。在此函数中,您可以在第 43 行的此语句之后执行以下操作:$config = $this->parseConfig($config, $name);
$config['database'] = 'Insert the name of your database here';
This change will allow Laravel to continue with its normal process having no negative effect anywhere with the framework or your project. That is until this bug is fixed by the makers of Laravel or the community.
此更改将允许 Laravel 继续其正常过程,而不会对框架或您的项目的任何地方产生负面影响。直到这个错误被 Laravel 的制造者或社区修复。
Till then happy coding! =)
直到那时快乐编码!=)