Laravel 迁移更改/更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37022384/
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
Laravel Migrations change/update
提问by John Does Legacy
How do I properly change / update my migration, I am using Laravel 5.1.
如何正确更改/更新我的迁移,我使用的是 Laravel 5.1。
The Docs said that I should use change to update the column e.g.:
文档说我应该使用更改来更新列,例如:
$table->string('color', 10)->change();
But where do I have to put it in the migration and what command do I have to use to update just a simply php artisan migrate?
但是我必须把它放在迁移中的什么地方,我必须使用什么命令来更新一个简单的 php artisan migrate?
I tried this so far:
到目前为止我试过这个:
public function up()
{
Schema::create('products', function (Blueprint $table)) {
$table->string('color', 5);
});
Schema::table('products', function (Blueprint $table)) {
$table->string('color', 10)->change();
});
}
And used this command:
并使用了这个命令:
php artisan migrate
But it did not change.
但它没有改变。
回答by Alexey Mezenin
First, create new migration to change existing table column(s).
首先,创建新迁移以更改现有表列。
Then you should do this inside up()
method in your new migration:
然后你应该up()
在你的新迁移中执行这个内部方法:
Schema::table('products', function (Blueprint $table)) {
$table->string('color', 10)->change();
});
So, you'll have two migrations. First did already create products
table with wrong length of the color
string.
因此,您将有两次迁移。首先确实已经创建products
了color
字符串长度错误的表。
Second migration will change color
string length from 5 to 10.
第二次迁移会将color
字符串长度从 5更改为 10。
After you created seconds migration, run php artisan migrate
again and changes will apply to a table.
创建秒迁移后,php artisan migrate
再次运行,更改将应用于表。
回答by Grant
Just thought I'd sum up what I did to achieve this, to elaborate on the answer above.
只是想我会总结一下我为实现这一目标所做的工作,以详细说明上面的答案。
php artisan make:migration change_yourcolumn_to_float_in_yourtable
Then, inside of that migration, in the up()
method, I added something like the following:
然后,在迁移过程中,在up()
方法中,我添加了如下内容:
Schema::table('yourtable', function (Blueprint $table) {
$table->float('yourcolumn')->change(); // Changing from int to float, ie.
});
Then you may need to add a package, to make database modifications like this:
Enter into the console: composer require doctrine/dbal
然后你可能需要添加一个包,像这样进行数据库修改: 进入控制台: composer require doctrine/dbal
Then you can go ahead and php artisan migrate
to commit the change.
然后您可以继续并php artisan migrate
提交更改。