diff --git a/src/Commands/PivotMigrationMakeCommand.php b/src/Commands/PivotMigrationMakeCommand.php index 9f2e4c4..69a28d6 100644 --- a/src/Commands/PivotMigrationMakeCommand.php +++ b/src/Commands/PivotMigrationMakeCommand.php @@ -36,6 +36,36 @@ class PivotMigrationMakeCommand extends GeneratorCommand */ protected function getNameInput() { + return $this->parseName($this->getPivotTableName()); + } + /** + * Execute the console command. + * + * @return bool|null + */ + public function fire() + { + $name = $this->getNameInput(); + + $path = $this->getPath($name); + + // First we will check to see if the class already exists. If it does, we don't want + // to create the class and overwrite the user's code. So, we will bail out so the + // code is untouched. Otherwise, we will continue generating this class' files. + if ($this->alreadyExists($this->getNameInput())) { + $this->error($this->type.' already exists!'); + + return false; + } + + // Next, we will generate the path to the location where this class' file should get + // written. Then, we will build the class and make the proper replacements on the + // stub files so that it gets the correctly formatted namespace and class name. + $this->makeDirectory($path); + + $this->files->put($path, $this->buildClass($name)); + + $this->info($this->type.' created successfully.'); } /** diff --git a/src/Migrations/SchemaParser.php b/src/Migrations/SchemaParser.php index aa8a600..e5fbde9 100644 --- a/src/Migrations/SchemaParser.php +++ b/src/Migrations/SchemaParser.php @@ -28,10 +28,6 @@ public function parse($schema) if ($this->fieldNeedsForeignConstraint($segments)) { unset($segments['options']['foreign']); - // If the user wants a foreign constraint, then - // we'll first add the regular field. - $this->addField($segments); - // And then add another field for the constraint. $this->addForeignConstraint($segments); @@ -123,6 +119,20 @@ private function parseOptions($options) */ private function addForeignConstraint($segments) { + /* + * First generate the unsigned indexed integer field + */ + $field = sprintf( + "%s:integer:unsigned:index", + $segments['name'], + $this->getTableNameFromForeignKey($segments['name']) + ); + + $this->addField($this->parseSegments($field)); + + /* + * Then build the fk + */ $string = sprintf( "%s:foreign:references('id'):on('%s')", $segments['name'],