@@ -66,11 +66,12 @@ public function handle()
66
66
$ fields = $ this ->getFields ($ input ->fields , 'migration ' , $ input ->fieldsFile );
67
67
68
68
$ properites = $ this ->getTableProperties ($ fields , $ input );
69
-
69
+
70
70
$ this ->replaceSchemaUp ($ stub , $ this ->getSchemaUpCommand ($ input , $ properites ))
71
71
->replaceSchemaDown ($ stub , $ this ->getSchemaDownCommand ($ input ))
72
+ ->replaceClassName ($ stub , $ input ->className )
72
73
->makeDirectory ($ this ->getMigrationsPath ())
73
- ->makeFile ($ this ->getMigrationsPath () . $ input ->className , $ stub , $ input ->force );
74
+ ->makeFile ($ this ->getMigrationsPath () . $ this -> makeFileName ( $ input ->tableName ) , $ stub , $ input ->force );
74
75
}
75
76
76
77
/**
@@ -87,7 +88,7 @@ protected function getTableProperties(array $fields, $input)
87
88
->addFieldProperties ($ properties , $ fields )
88
89
->addIndexes ($ properties , $ input ->indexes )
89
90
->addForeignConstraints ($ properties , $ input ->keys );
90
-
91
+
91
92
return $ properties ;
92
93
}
93
94
@@ -336,9 +337,11 @@ protected function getCleanColumns($columnsString)
336
337
*/
337
338
protected function addFieldProperties (& $ properties , array $ fields )
338
339
{
340
+ $ primaryField = $ this ->getPrimaryField ($ fields );
341
+
339
342
foreach ($ fields as $ field )
340
343
{
341
- if ($ field instanceof Field)
344
+ if ($ field instanceof Field && $ field != $ primaryField )
342
345
{
343
346
$ this ->addFieldType ($ properties , $ field )
344
347
->addFieldComment ($ properties , $ field )
@@ -455,9 +458,7 @@ protected function getPropertyBaseSpace($multiplier = 12, $prependNewline = fals
455
458
protected function getSchemaDownCommand ($ input )
456
459
{
457
460
458
- $ stub = <<<EOD
459
- Schema::{{connectionName}}drop('{{tableName}}');
460
- EOD ;
461
+ $ stub = $ this ->getStubContent ('schema-down ' );
461
462
462
463
$ this ->replaceConnectionName ($ stub , $ input ->connection )
463
464
->replaceTableName ($ stub , $ input ->tableName );
@@ -475,19 +476,32 @@ protected function getSchemaDownCommand($input)
475
476
protected function getSchemaUpCommand ($ input , $ blueprintBody )
476
477
{
477
478
478
- $ stub = <<<EOD
479
- Schema::{{connectionName}}create('{{tableName}}', function(Blueprint \$table)
480
- {
481
- {{blueprintBody}}
482
- });
483
- EOD ;
479
+ $ stub = $ this ->getStubContent ('schema-up ' );
480
+
484
481
$ this ->replaceConnectionName ($ stub , $ input ->connection )
485
482
->replaceTableName ($ stub , $ input ->tableName )
486
483
->replaceBlueprintBodyName ($ stub , $ blueprintBody );
487
484
488
485
return $ stub ;
489
486
}
490
487
488
+
489
+
490
+ /**
491
+ * Replace the className of the given stub.
492
+ *
493
+ * @param string $stub
494
+ * @param string $className
495
+ *
496
+ * @return $this
497
+ */
498
+ protected function replaceClassName (&$ stub , $ className )
499
+ {
500
+ $ stub = str_replace ('DummyClass ' , $ className , $ stub );
501
+
502
+ return $ this ;
503
+ }
504
+
491
505
/**
492
506
* Replace the blueprintBody for the given stub.
493
507
*
@@ -666,8 +680,8 @@ protected function addPrimaryField(& $property, Field $field)
666
680
{
667
681
if (!is_null ($ field ))
668
682
{
669
- $ primaryMethod = trim ( $ this ->getPropertyBase ( $ this -> getPrimaryMethodName ($ field ->dataType )) );
670
- $ property .= sprintf ("%s('%s') " , $ primaryMethod , $ field ->name );
683
+ $ eloquentMethodName = $ this ->getPrimaryMethodName ($ field ->dataType );
684
+ $ property .= sprintf ("%s('%s') " , $ this -> getPropertyBase ( $ eloquentMethodName ) , $ field ->name );
671
685
$ this ->addFieldPropertyClousure ($ property );
672
686
}
673
687
@@ -711,19 +725,35 @@ protected function getPrimaryMethodName($type)
711
725
protected function getCommandInput ()
712
726
{
713
727
$ tableName = trim ($ this ->argument ('table-name ' ));
714
- $ className = trim ($ this ->option ('migration-class-name ' )) ?: sprintf ('%s_create_%s_table.php ' , date ('Y_m_d_His ' ), strtolower ($ tableName ));
728
+
729
+ $ className = trim ($ this ->option ('migration-class-name ' )) ?: sprintf ('Create%sTable ' , ucfirst ($ tableName ));
715
730
$ connection = trim ($ this ->option ('connection-name ' ));
716
731
$ engine = trim ($ this ->option ('engine-name ' ));
717
732
$ fields = trim ($ this ->option ('fields ' ));
718
733
$ fieldsFile = trim ($ this ->option ('fields-file ' ));
719
734
$ indexes = $ this ->getIndexColelction (trim ($ this ->option ('indexes ' )));
720
735
$ force = $ this ->option ('force ' );
721
736
$ keys = $ this ->getKeysCollections (trim ($ this ->option ('foreign-keys ' )));
722
- $ className = Helpers:: postFixWith ( $ className , ' .php ' );
737
+
723
738
724
739
return (object ) compact ('tableName ' ,'className ' ,'connection ' ,'engine ' ,'fields ' ,'fieldsFile ' ,'force ' ,'indexes ' ,'keys ' );
725
740
}
726
741
742
+
743
+ /**
744
+ * Makes a file name for the migration
745
+ *
746
+ * @param string $path
747
+ * @return $this
748
+ */
749
+ protected function makeFileName ($ tableName )
750
+ {
751
+ $ filename = sprintf ('%s_create_%s_table.php ' , date ('Y_m_d_His ' ), strtolower ($ tableName ));
752
+
753
+ return Helpers::postFixWith ($ filename , '.php ' );
754
+ }
755
+
756
+
727
757
/**
728
758
* Build the directory for the class if necessary.
729
759
*
0 commit comments