Schema Builder整数的长度

问题描述 投票:11回答:6

我一直在四处寻找,问了几次问题,但似乎没有人能给出明确的答案。如何使用Schema指定表列的整数长度?

我见过有人建议:

$table->integer('post')->length(11);

但这不起作用 - 至少在Laravel 4.2中 - 它仍然产生int(10)列。

是否有内置方法来指定整数长度?

laravel-4 database-schema
6个回答
17
投票

如果您使用的是MySQL,则无法指定整数列的长度。您只能在http://dev.mysql.com/doc/refman/5.1/en/integer-types.html中描述的一种可用整数类型之间进行选择。

因此,您也无法在Laravel中设置整数长度。您只能选择http://laravel.com/docs/schema#adding-columns中描述的其中一种可用类型。


4
投票

我猜你要指定长度为10以匹配增量id(在声明外键时)。如果是这样,那么你必须使用:

$table->unsignedInteger('some_id_reference');

2
投票

我以为我会为一般情况表创建一个易于复制和粘贴的方法。 如果您确实需要负值,则签名,如果不需要,则签名。

| Type                | Eloquent (Schema Builder)                 | Min     | Max    |
| ------------------- | ----------------------------------------- | ------- | ------ |
| TINYINT (Signed)    | $table->signedTinyInteger('foo')          | -128    | 127    |
| TINYINT (Unsigned)  | $table->unsignedTinyInteger('foo')        | 0       | 255    |
| SMALLINT (Signed)   | $table->signedSmallInteger('foo')         | -32768  | 32767  |
| SMALLINT (Unsigned) | $table->unsignedSmallInteger('foo')       | 0       | 65535  |

有关较大的Integer类型,请参阅:https://dev.mysql.com/doc/refman/5.5/en/integer-types.html


0
投票

现在,在Laravel 5中:

$table->addColumn('integer', 'post', ['length' => '10']); // Creates INT(10)
$table->addColumn('integer', 'post', ['length' => '10'])->unsigned(); // Creates Unsigned INT(10)
$table->unsignedInteger('post'); // Creates Unsigned INT(10)

$table->integer('post'); // Creates INT(11)
$table->integer('post')->unsigned(); // Creates Unsigned INT(11)

0
投票
$table->bigInteger('variable');
$table->integer('variable');
$table->mediumInteger('variable'); 
$table->smallInteger('variable');
$table->tinyInteger('variable');
$table->unsignedBigInteger('variable');
$table->unsignedMediumInteger('variable'); 
$table->unsignedSmallInteger('variable');  
$table->unsignedTinyInteger('variable');  

https://laravel.com/docs/5.7/migrations


0
投票

是的,可以使用以下命令更改默认列的长度:

$table->string('any_text',35)->change();

您还可以使用以下内容使其可以为空:

$table->string('any_text',35)->nullable()->change();

我希望这个对你有用 :)

© www.soinside.com 2019 - 2024. All rights reserved.