5

How can I add tiny integer column using laravel migration to MySQL? I thought this code

$table->addColumn('tinyInteger', 'birth_day', ['lenght' => 2]);

But it creates TINYINT(4) column. I don't know how I can solve this problem. Please don't ask me why a day only, not full date. It is a business logic of the app.

Lukas Pierce
  • 807
  • 3
  • 9
  • 15
  • Try: `$table->addColumn('unsignedTinyInteger', 'birth_day', ['lenght' => 2]);` – Jorge Campos Aug 21 '17 at 18:42
  • It doesn't work, Error: `Call to undefined method Illuminate\Database\Schema\Grammars\MySqlGrammar::typeUnsignedTinyInteger()` – Lukas Pierce Aug 21 '17 at 18:55
  • Laravel migrations aren't MySQL specific so if you're developing a project where you won't control the database layer, I wouldn't recommend creating a migration specific to MySQL. This will also hamper your testing environments as most devs will use sqlite. – Devon Bessemer Aug 21 '17 at 19:11

3 Answers3

4

It looks to me like you're spelling "length" wrong, maybe try $table->addColumn('tinyInteger', 'birth_day', ['length' => 2]);

Andrew Fox
  • 794
  • 4
  • 13
  • 30
2

I solve my problem by pure sql

DB::statement("ALTER TABLE `users` 
ADD `birth_day` TINYINT(2) DEFAULT NULL AFTER `lastname`");
Lukas Pierce
  • 807
  • 3
  • 9
  • 15
2
   /**
     * Create a new tiny integer (1-byte) column on the table.
     *
     * @param  string  $column
     * @param  bool  $autoIncrement
     * @param  bool  $unsigned
     * @return \Illuminate\Support\Fluent
     */
    public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
    {
        return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
    }

This is the tinyInteger() function from Blueprint.php. As you can see it expects a boolean parameter here. It looks like you're trying to add a argument for size. You cannot specify the size of tinyInt in Laravel.

Instead use

$table->tinyInteger('birth_day'); // `birth_day` tinyint(3)
Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62