Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with pivot table migration class name in 5.4 #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Commands/PivotMigrationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/Migrations/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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'],
Expand Down