Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 219
Hax0r Avatar asked Dec 27 '17 07:12

Hax0r


People also ask

How do I change the default value in Laravel migration?

In our case we are going to do second option - create new migration. If we roll back this migration, we need to remove default value from that field using migration and this is how to do it: Schema::table('photos', function (Blueprint $table) { $table->integer('order')->default(NULL)->change(); });


4 Answers

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

like image 128
Alexey Mezenin Avatar answered Sep 20 '22 09:09

Alexey Mezenin


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');   
like image 21
mfadel Avatar answered Sep 20 '22 09:09

mfadel


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');
like image 34
oriberu Avatar answered Sep 17 '22 09:09

oriberu


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."'");
like image 37
Carlos Ivan Olvidaos Avatar answered Sep 18 '22 09:09

Carlos Ivan Olvidaos