Laravel - 无符号可空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41592288/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:06:24  来源:igfitidea点击:

Laravel - unsigned with nullable

phplaravellaravel-5migration

提问by Learner

I am using Laravel 5.3, I want to define a field in table migration as nullableas well as unsigned. As both are index modifier, can I use them in concatenation ? Like:

我正在使用 Laravel 5.3,我想将表迁移中的字段定义为可空unsigned。由于两者都是索引修饰符,我可以将它们串联使用吗?喜欢:

$table->integer('some_field')->unsigned()->nullable();

Please also give some reference if there is to these kind of modifications in laravel documentation or somewhere.

如果在 laravel 文档或其他地方有这些修改,也请提供一些参考。

Please note that I want to define field in up()function as unsigned as well as nullable. I dont want solutions with down()function like:

请注意,我想将up()函数中的字段定义为无符号和可为空。我不想要具有以下down()功能的解决方案:

public function up()
    {
       Schema::create('ex', function (Blueprint $table) {
            $table->integer('some_field')->unsigned();
       });
    }
public function down()
    {
        DB::statement('ALTER TABLE ex MODIFY `some_field` integer NOT NULL;');
    }

Thanks in advance!

提前致谢!

采纳答案by shalvah

You can. Laravel allows a lot of method-chaining. Each of the column methods is defined to return the same Blueprintobject, so you can easily call another method on it. This means you could even do:

你可以。Laravel 允许很多方法链。每个列方法都定义为返回相同的Blueprint对象,因此您可以轻松地对其调用另一个方法。这意味着你甚至可以这样做:

Schema::create('ex', function (Blueprint $table) { 
  $table->integer('some_field')->unsigned()->default(10); 
});

And all would be well :)

一切都会好起来的:)

For further information, see the documentation on database migrations(see the section on "Column Modifiers").

有关详细信息,请参阅有关数据库迁移的文档(请参阅“列修饰符”部分)。

回答by Antonio Carlos Ribeiro

You can do

你可以做

   Schema::create('ex', function (Blueprint $table) {
        $table->integer('some_field')->unsigned()->nullable();
   });

回答by Learner

Although this question has been answered. I also chose the accepted answer which confirms that using:

虽然这个问题已经回答了。我还选择了接受的答案,确认使用:

$table->integer('some_field')->unsigned()->nullable();

is right.

是对的。

Although I am listing one more way to do same:

虽然我列出了另一种方法:

$table->integer('some_field')->unsigned();
$table->integer('some_field')->nullable()->change();

with reference : https://laravel.com/docs/5.3/migrations#modifying-columns

参考:https: //laravel.com/docs/5.3/migrations#modifying-columns