Schema Builder length of an integer

前端 未结 6 914
一整个雨季
一整个雨季 2021-01-01 21:17

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

相关标签:
6条回答
  • 2021-01-01 21:41

    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

    0 讨论(0)
  • 2021-01-01 21:42
    $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 讨论(0)
  • 2021-01-01 21:45

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

    0 讨论(0)
  • 2021-01-01 21:53

    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.

    0 讨论(0)
  • 2021-01-01 21:55

    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');
    
    0 讨论(0)
  • 2021-01-01 21:58

    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)
    
    0 讨论(0)
提交回复
热议问题