diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 471c91c0..b39c4609 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -203,7 +203,13 @@ protected function buildDefinition(Model $model) foreach ($modifiers as $modifier) { if (is_array($modifier)) { - $column_definition .= sprintf("->%s('%s')", key($modifier), addslashes(current($modifier))); + $modifierKey = key($modifier); + $modifierValue = addslashes(current($modifier)); + if (in_array($dataType, ['boolean', 'tinyinteger']) && $modifierKey === 'default') { + $column_definition .= sprintf("->%s(%s)", $modifierKey, $modifierValue); + } else { + $column_definition .= sprintf("->%s('%s')", $modifierKey, $modifierValue); + } } elseif ($modifier === 'unsigned' && Str::startsWith($dataType, 'unsigned')) { continue; } elseif ($modifier === 'nullable' && Str::startsWith($dataType, 'nullable')) { diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 314b628a..35fb9709 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -868,6 +868,7 @@ public function modelTreeDataProvider() ['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'], ['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'], ['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'], + ['drafts/boolean-column-default.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/boolean-column-default.php'], ]; } } diff --git a/tests/fixtures/drafts/boolean-column-default.yaml b/tests/fixtures/drafts/boolean-column-default.yaml new file mode 100644 index 00000000..4b54418a --- /dev/null +++ b/tests/fixtures/drafts/boolean-column-default.yaml @@ -0,0 +1,8 @@ +models: + Post: + title: string:400 + content: longtext + show: boolean default:1 + hide: boolean default:0 + draft: boolean default:true + published: boolean default:false diff --git a/tests/fixtures/migrations/boolean-column-default.php b/tests/fixtures/migrations/boolean-column-default.php new file mode 100644 index 00000000..aa0d913c --- /dev/null +++ b/tests/fixtures/migrations/boolean-column-default.php @@ -0,0 +1,37 @@ +id(); + $table->string('title', 400); + $table->longText('content'); + $table->boolean('show')->default(1); + $table->boolean('hide')->default(0); + $table->boolean('draft')->default(true); + $table->boolean('published')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('posts'); + } +}