How can I define property zerofill and size (2) on field schema migration with Laravel?
Schema::create(\'books\', function (Blueprint $table) {
$table-&g
Zerofill is not a SQL standard. The shema builder of laravel provides only these ANSI SQL standards.
But you can use a workaround to define it with a raw sql statement:
create_books.php
Schema::create('books', function (Blueprint $table) {
$table->integer('reference');
});
DB::statement('ALTER TABLE books CHANGE reference reference INT(2) UNSIGNED ZEROFILL NOT NULL');
As mentioned, in order to keep your migrations portable, you'll want to use an accessor to provide that. It'll look something like this:
Book.php model
public function getReferenceAttribute($value)
{
if($value){
return str_pad($value, 2, '0', STR_PAD_LEFT);
}else{
return null;
}
}