Hax0r
Hax0r

Reputation: 1803

Laravel 5.5 set size of integer fields in migration file

I am new to laravel.

Now i am using the migrate command to make a table, but the length of the filed is not applicable. Laravel does not provide this option. ?

Below is my codes:

$table->increments('id')->length(11);
$table->dateTime('created_time');
$table->integer('bank_id')->length(11);
$table->tinyInteger('is_black')->length(1);

The length of the field is_black should be 1, but it is actually being generated as 4. How can I solve this problem ?

Any suggestion or advice would be appreciated.

Thank you in advance

Upvotes: 23

Views: 94270

Answers (8)

Hans Paul
Hans Paul

Reputation: 11

In Laravel 10, I did not find any other way to get tinyint(1), but this:

public function up(): void
{
    Schema::create('country', function (Blueprint $table) {
        // This generate tinyint(4) default 1
        $table->tinyInteger('active', false, true)->default(1);
        $table->timestamps();
    });
    // This generate tinyint(1) default 1
    DB::statement('ALTER TABLE `country` ADD `active2` TINYINT(1) NOT NULL DEFAULT 1');
}

Upvotes: 0

akituti
akituti

Reputation: 41

This code works for me.

$table->addColumn(
    'tinyInteger', 'field_name',
    [
        'length'   => 2,
        'default'  => '1',
        'autoIncrement' => false,
        'unsigned' => true,
        'comment'  => 'Some comments'
    ]
);

Upvotes: 2

Carlos Ivan Olvidaos
Carlos Ivan Olvidaos

Reputation: 433

this is solution for me! Inside on function run.

    $tableName = 'tblresefeage';
    $comments = 'Resumen efectividad por agencia';
    Schema::create($tableName, function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('agencia')->comment('Agencia');
        $table->date('fechacierre')->comment('Fecha cierre');           
        $table->timestamps();
    });
    DB::statement('ALTER TABLE tblresefeage MODIFY COLUMN agencia INTEGER (11);');

    Schema::table($tableName, function (Blueprint $table) {
        $table->foreign('agencia')->on('tblentage')->references('cveentage')->onDelete('cascade');
    });

    DB::statement("ALTER TABLE `$tableName` comment '".$comments."'");

Upvotes: 4

user12255561
user12255561

Reputation: 23

$table->increments('id',11);
$table->dateTime('created_time');
$table->integer('bank_id',11);
$table->tinyInteger('is_black',1);

Upvotes: -5

oriberu
oriberu

Reputation: 1216

According to https://laravel.com/docs/5.1/migrations, as of Laravel 5.1 you can use the boolean column type to create a "boolean-like" TINYINT of length 1 (MySQL). So, for example:

$table->boolean('nameOfColumn');

Upvotes: 8

quoctinh0897
quoctinh0897

Reputation: 25

You can use this way. Good luck.

$table->decimal('is_black',1,0);

Upvotes: 0

mfadel
mfadel

Reputation: 897

According to https://laravel.com/docs/5.5/migrations, you can use one of these types:

$table->bigInteger('votes');
$table->integer('votes');

$table->mediumInteger('votes'); 
$table->smallInteger('votes');
$table->tinyInteger('votes');
$table->unsignedBigInteger('votes');
$table->unsignedMediumInteger('votes'); 
$table->unsignedSmallInteger('votes');  
$table->unsignedTinyInteger('votes');   

Upvotes: 6

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You can't do this, but you can use different types of integer:

$table->bigInteger()
$table->mediumInteger()
$table->integer()
$table->smallInteger()
$table->tinyInteger()

https://laravel.com/docs/5.5/migrations#columns

Upvotes: 32

Related Questions