Skip to content

Commit 9a02f2a

Browse files
ghostwriteriMokhlesNathan Esayeas
authored
Fix boolean column default modifier (#426)
Co-authored-by: Mokhlas Hussein <[email protected]> Co-authored-by: Nathan Esayeas <[email protected]>
1 parent 98d43c3 commit 9a02f2a

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,13 @@ protected function buildDefinition(Model $model)
211211

212212
foreach ($modifiers as $modifier) {
213213
if (is_array($modifier)) {
214-
$column_definition .= sprintf("->%s('%s')", key($modifier), addslashes(current($modifier)));
214+
$modifierKey = key($modifier);
215+
$modifierValue = addslashes(current($modifier));
216+
if (in_array($dataType, ['boolean', 'tinyinteger']) && $modifierKey === 'default') {
217+
$column_definition .= sprintf("->%s(%s)", $modifierKey, $modifierValue);
218+
} else {
219+
$column_definition .= sprintf("->%s('%s')", $modifierKey, $modifierValue);
220+
}
215221
} elseif ($modifier === 'unsigned' && Str::startsWith($dataType, 'unsigned')) {
216222
continue;
217223
} elseif ($modifier === 'nullable' && Str::startsWith($dataType, 'nullable')) {

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ public function modelTreeDataProvider()
870870
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
871871
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],
872872
['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'],
873+
['drafts/boolean-column-default.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/boolean-column-default.php'],
873874
];
874875
}
875876
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
models:
2+
Post:
3+
title: string:400
4+
content: longtext
5+
show: boolean default:1
6+
hide: boolean default:0
7+
draft: boolean default:true
8+
published: boolean default:false
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePostsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('posts', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('title', 400);
19+
$table->longText('content');
20+
$table->boolean('show')->default(1);
21+
$table->boolean('hide')->default(0);
22+
$table->boolean('draft')->default(true);
23+
$table->boolean('published')->default(false);
24+
$table->timestamps();
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::dropIfExists('posts');
36+
}
37+
}

0 commit comments

Comments
 (0)