laravel 添加迁移以将整数设置为无符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21754644/
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
Add migration to set integer to unsigned
提问by babbaggeii
I've got a table, and I want to add ->unsigned() to the id. At the moment, the migration looks like: $table->increments('id');
我有一张表,我想将 ->unsigned() 添加到 ID。目前,迁移看起来像:$table->increments('id');
So I want to do a new migration to set that to unsigned, but there's not much in the documentation to do this. Is this the correct way:
所以我想做一个新的迁移以将其设置为未签名,但文档中没有太多内容可以做到这一点。这是正确的方法吗:
public function up()
{
Schema::table('authors', function($t) {
$t->unsigned('id', 'authors_id_unsigned');
});
}
public function down()
{
Schema::table('authors', function($t) {
$t->dropUnsigned('authors_id_unsigned');
});
}
I'm just guessing here because I can't find it in the docs.
我只是在这里猜测,因为我在文档中找不到它。
回答by Dane
You can modify columns in Laravel 5+, but you have to add the doctrine/dbal dependency to your composer.json file.
你可以在 Laravel 5+ 中修改列,但是你必须在你的 composer.json 文件中添加学说/dbal 依赖项。
public function up()
{
Schema::table('my_table', function (Blueprint $table) {
$table->integer('my_column')->unsigned()->change();
});
}
public function down()
{
Schema::table('my_table', function (Blueprint $table) {
$table->integer('my_column')->change();
});
}
回答by Repox
You can't change that kind of details with the Schema Builder.
您无法使用 Schema Builder 更改此类详细信息。
To change it, you have to run a raw query. To achieve this, your migration should look something like this:
要更改它,您必须运行原始查询。为此,您的迁移应如下所示:
public function up()
{
Schema::table('authors', function($t) {
DB::statement("ALTER TABLE `authors` CHANGE COLUMN `id` `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT FIRST");
});
}
public function down()
{
Schema::table('authors', function($t) {
DB::statement("ALTER TABLE `authors` CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT FIRST");
});
}