23

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

2
  • 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. Commented Aug 3, 2020 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 Commented Dec 13, 2022 at 12:20

8 Answers 8

33

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

Sign up to request clarification or add additional context in comments.

Comments

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');

Comments

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');   

4 Comments

There are 5 types actually.
@AlexeyMezenin thanks, you are right there are more integer types, actually, with the unsigned versions, there are more than 5 types.
unsigned is just a shortcut for integer()->unsigned() so it's not a type.
OK, but how is this possible to choose length ?
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."'");

Comments

2

This code works for me.

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

1 Comment

Can you improve the formatting of your suggested code through code blocks?
0

You can use this way. Good luck.

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

Comments

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');
}

Comments

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

4 Comments

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.
Also, this doesn't work. Have you read it somewhere in docs that a second parameter could be passed?
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')
The second parameter on the integer method is for flagging autoIncrement as true, not setting the length of the field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.