Skip to content

Commit e3dab93

Browse files
committed
Fix the migration class for Laravel 5.7
1 parent 1096a58 commit e3dab93

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

src/Commands/Framework/CreateModelCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ protected function getAccessors(array $fields = null)
500500
$accessors[] = $this->getAccessor($field, $content);
501501
}
502502

503-
if ($field->isDateOrTime()) {
503+
if ($field->isDateOrTime() && !$field->isDate) {
504+
// We should not create accessor for a datetime field that is casted to Carbon object
504505
$content = $this->getStubContent('model-accessor-datetime');
505506
$this->replaceDateFormat($content, $field->dateFormat);
506507

src/Commands/Migrations/MigrateAllCommand.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CrestApps\CodeGenerator\Commands\Migrations;
44

55
use CrestApps\CodeGenerator\Commands\Bases\MigrationCommandBase;
6+
use CrestApps\CodeGenerator\Support\Helpers;
67
use Illuminate\Console\ConfirmableTrait;
78
use Illuminate\Database\Migrations\Migrator;
89

@@ -42,19 +43,31 @@ public function handle()
4243

4344
$this->prepareDatabase();
4445

45-
// Next, we will check to see if a path option has been defined. If it has
46-
// we will use the path relative to the root of this installation folder
47-
// so that migrations may be run for any path within the applications.
48-
$this->migrator->run($this->getMigrationPaths(), [
49-
'pretend' => $this->option('pretend'),
50-
'step' => $this->option('step'),
51-
]);
46+
if (Helpers::isNewerThanOrEqualTo('5.7')) {
47+
// Next, we will check to see if a path option has been defined. If it has
48+
// we will use the path relative to the root of this installation folder
49+
// so that migrations may be run for any path within the applications.
5250

53-
// Once the migrator has run we will grab the note output and send it out to
54-
// the console screen, since the migrator itself functions without having
55-
// any instances of the OutputInterface contract passed into the class.
56-
foreach ($this->migrator->getNotes() as $note) {
57-
$this->output->writeln($note);
51+
$this->migrator->setOutput($this->output)
52+
->run($this->getMigrationPaths(), [
53+
'pretend' => $this->option('pretend'),
54+
'step' => $this->option('step'),
55+
]);
56+
} else {
57+
// Next, we will check to see if a path option has been defined. If it has
58+
// we will use the path relative to the root of this installation folder
59+
// so that migrations may be run for any path within the applications.
60+
$this->migrator->run($this->getMigrationPaths(), [
61+
'pretend' => $this->option('pretend'),
62+
'step' => $this->option('step'),
63+
]);
64+
65+
// Once the migrator has run we will grab the note output and send it out to
66+
// the console screen, since the migrator itself functions without having
67+
// any instances of the OutputInterface contract passed into the class.
68+
foreach ($this->migrator->getNotes() as $note) {
69+
$this->output->writeln($note);
70+
}
5871
}
5972

6073
// Finally, if the "seed" option has been given, we will re-run the database

src/Traits/Migration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function getRan()
101101
*/
102102
protected function getAlterMigrationName($name, $count)
103103
{
104-
$filename = sprintf('%s_alter_%s_table_%s.php', date('Y_m_d_His'), strtolower($name), $count);
104+
$filename = sprintf('%s_alter_%s_%s_table.php', date('Y_m_d_His'), strtolower($name), $count);
105105

106106
return Str::postfix($filename, '.php');
107107
}
@@ -142,6 +142,6 @@ protected function makeCreateTableClassName($tableName)
142142
*/
143143
protected function makeAlterTableClassName($tableName, $id)
144144
{
145-
return sprintf('Alter%sTable%s', studly_case($tableName), $id);
145+
return sprintf('Alter%s%sTable', studly_case($tableName), $id);
146146
}
147147
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
return \DateTime::createFromFormat('[% date_format %]', $value);
1+
return \DateTime::createFromFormat($this->getDateFormat(), $value)->format('[% date_format %]');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$this->attributes['[% field_name %]'] = !empty($value) ? \DateTime::createFromFormat($this->getDateFormat(), $value) : null;
1+
$this->attributes['[% field_name %]'] = !empty($value) ? \DateTime::createFromFormat('[% date_format %]', $value) : null;

0 commit comments

Comments
 (0)