Schema Builder整数的长度

时间:2014-09-10 18:42:13

标签: laravel-4 database-schema

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

我见过有人建议:

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

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

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

6 个答案:

答案 0 :(得分:16)

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

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

答案 1 :(得分:4)

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

$table->unsignedInteger('some_id_reference');

答案 2 :(得分:1)

我认为我会为一般情况表创建一个易于复制和粘贴的表格。
如果您 ,则

| 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  |

对于较大的整数类型,请参阅:https://dev.mysql.com/doc/refman/5.5/en/integer-types.html

答案 3 :(得分: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)

答案 4 :(得分: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

答案 5 :(得分:0)

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

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

您还可以使用以下方法将其设置为空:

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

我希望它对您有用:)