19

I've been searching around and the question was asked a few times, but no-one seem to be able to give a definite answer to it. How do you specify the integer length for the table column using Schema?

I've seen someone suggesting:

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

But that doesn't work - at least with Laravel 4.2 - it still produces the column as int(10).

Is there a built in method to specify the integer length?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Spencer Mark
  • 5,263
  • 9
  • 29
  • 58

7 Answers7

20

If you're using MySQL, you can't specify the length of an integer column. You can only choose between one of the available integer types, described at http://dev.mysql.com/doc/refman/5.1/en/integer-types.html.

Hence, you cannot set the integer length in Laravel either.

You can only choose one of the available types described at Laravel 4.2 Database Migration Creating Column.

anasey
  • 169
  • 12
lowerends
  • 5,469
  • 2
  • 24
  • 36
  • 2
    But MySQL's integer length goes up to 11 - not 10, which seem to be the default one? Same for TINYINT which goes up to 4 etc. – Spencer Mark Sep 10 '14 at 18:56
  • 13
    It goes up to 11 for signed integers. For unsigned integers (and then I mean the `INT` type), it goes up to 10. – lowerends Sep 10 '14 at 18:59
  • 1
    Makes sense - thanks, although it's a shame that you cannot specify the length - it could be useful in some scenarios. – Spencer Mark Sep 10 '14 at 19:02
  • 3
    Actually, you **can** specify the length of an integer column in MySQL. See the [Numeric Type Attributes](http://dev.mysql.com/doc/refman/5.1/en/numeric-type-attributes.html) of the same docs.The _length_ in this case is not the size of the data but the display width of the output. Typically, boolean columns will be stored as `TINYINT(1)` even though the `TINYINT` can have a display width of up to 4. – jeteon Mar 25 '15 at 02:11
14

Thought I'd create an easy-to-copy-and-paste for general situations table.
Signed if you do require negative values and unsigned if you do not.

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

For larger Integer types, see: https://dev.mysql.com/doc/refman/5.5/en/integer-types.html

Grant
  • 5,709
  • 2
  • 38
  • 50
4

I'm guessing that you want to specify a length of 10 to match an increment id (when declaring foreign keys). If so then you have to use:

$table->unsignedInteger('some_id_reference');
omarjebari
  • 4,861
  • 3
  • 34
  • 32
2

Now, in 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)
LowLevel
  • 1,085
  • 1
  • 13
  • 34
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

Berthold Feujo
  • 338
  • 5
  • 12
0

Table 11.1 Required Storage and Range for Integer Types Supported by MySQL

I think it will help for you.

enter image description here

Zahid Hassan Shaikot
  • 1,066
  • 10
  • 18
-1

yes, it is possible to change the length of a default column by using:

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

you can also make it nullable by using:

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

I hope it works for you :)

Zia Khan
  • 11
  • 4