Create a tiny integer column with custom size in Laravel migration
Clash Royale CLAN TAG#URR8PPP
Create a tiny integer column with custom size in Laravel migration
How can I add tiny integer column using laravel migration to MySQL? I thought this code
$table->addColumn('tinyInteger', 'birth_day', ['lenght' => 2]);
$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.
$table->addColumn('unsignedTinyInteger', 'birth_day', ['lenght' => 2]);
It doesn't work, Error:
Call to undefined method IlluminateDatabaseSchemaGrammarsMySqlGrammar::typeUnsignedTinyInteger()
– Lukas Pierce
Aug 21 '17 at 18:55
Call to undefined method IlluminateDatabaseSchemaGrammarsMySqlGrammar::typeUnsignedTinyInteger()
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
Aug 21 '17 at 19:11
2 Answers
2
I solve my problem by pure sql
DB::statement("ALTER TABLE `users`
ADD `birth_day` TINYINT(2) DEFAULT NULL AFTER `lastname`");
/**
* Create a new tiny integer (1-byte) column on the table.
*
* @param string $column
* @param bool $autoIncrement
* @param bool $unsigned
* @return IlluminateSupportFluent
*/
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)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try:
$table->addColumn('unsignedTinyInteger', 'birth_day', ['lenght' => 2]);
– Jorge Campos
Aug 21 '17 at 18:42