21

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

Hax0r
  • 1,722
  • 4
  • 25
  • 43
  • 1
    As I see, you want an 11 length integer. This is a common mistake when you use an unsigned integer. Not unsigned integers are 11 lengths, and unsigned ones are 10 lengths. So try setting the unsigned flag to false, or removing the ->unsigned() operator. – JuliSmz Aug 03 '20 at 21:31
  • after adding table schema, you can add the DB::statement query and add here the ALTER query for changing the column type and length. Currently we can do this only to produce expected result – Amit Kumar Dec 13 '22 at 12:20

8 Answers8

31

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

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
8

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');
oriberu
  • 1,186
  • 9
  • 6
6

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');   
mfadel
  • 887
  • 12
  • 32
4

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."'");
2

This code works for me.

$table->addColumn(
    'tinyInteger', 'field_name',
    [
        'length'   => 2,
        'default'  => '1',
        'autoIncrement' => false,
        'unsigned' => true,
        'comment'  => 'Some comments'
    ]
);
commadelimited
  • 5,656
  • 6
  • 41
  • 77
akituti
  • 41
  • 4
0

You can use this way. Good luck.

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

quoctinh0897
  • 25
  • 1
  • 1
0

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');
}
Hans Paul
  • 1
  • 3
-5
$table->increments('id',11);
$table->dateTime('created_time');
$table->integer('bank_id',11);
$table->tinyInteger('is_black',1);
user12255561
  • 23
  • 1
  • 1
  • 2
    From review: Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it solves the problem. – BDL Oct 22 '19 at 08:16
  • 4
    Also, this doesn't work. Have you read it somewhere in docs that a second parameter could be passed? – Muhammad Noman Nov 14 '19 at 04:57
  • 1
    This code is not working, if I put a second argument, I got this error : SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table `tvas` (`id` bigint unsigned not null auto_increment primary key, `taux` int not null auto_increment primary key, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci') – David Apr 12 '20 at 15:48
  • 2
    The second parameter on the `integer` method is for flagging autoIncrement as true, not setting the length of the field. – K.B. Aug 01 '20 at 18:37