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

Add schema to Pivot table #114

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "laracasts/generators",
"name": "yolcar/generators",
"description": "Extend Laravel 5's generators.",
"keywords": ["laravel", "generators"],
"license": "MIT",
Expand Down
39 changes: 34 additions & 5 deletions src/Commands/PivotMigrationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Laracasts\Generators\Commands;

use Illuminate\Console\GeneratorCommand;
use Laracasts\Generators\Migrations\NameParser;
use Laracasts\Generators\Migrations\SchemaParser;
use Laracasts\Generators\Migrations\SyntaxBuilder;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -29,6 +32,13 @@ class PivotMigrationMakeCommand extends GeneratorCommand
*/
protected $type = 'Migration';

/**
* Meta information for the requested migration.
*
* @var array
*/
protected $meta;

/**
* Get the desired class name from the input.
*
Expand Down Expand Up @@ -59,7 +69,11 @@ protected function parseName($name)
*/
protected function getStub()
{
return __DIR__ . '/../stubs/pivot.stub';
if ($this->option('schema')) {
return __DIR__ . '/../stubs/pivot-schema.stub';
}else{
return __DIR__ . '/../stubs/pivot.stub';
}
}

/**
Expand All @@ -71,7 +85,7 @@ protected function getStub()
protected function getPath($name = null)
{
return base_path() . '/database/migrations/' . date('Y_m_d_His') .
'_create_' . $this->getPivotTableName() . '_pivot_table.php';
'_create_' . $this->getPivotTableName() . '_pivot_table.php';
}

/**
Expand All @@ -83,7 +97,6 @@ protected function getPath($name = null)
protected function buildClass($name = null)
{
$stub = $this->files->get($this->getStub());

return $this->replacePivotTableName($stub)
->replaceSchema($stub)
->replaceClass($stub, $name);
Expand Down Expand Up @@ -118,9 +131,14 @@ protected function replaceSchema(&$stub)
$stub
);

if ($schema = $this->option('schema')) {
$schema = (new SchemaParser)->parse($schema);
$stub = (new SyntaxBuilder)->createPivotSchema($schema,$stub);
}

return $this;
}

/**
* Replace the class name for the given stub.
*
Expand All @@ -131,7 +149,6 @@ protected function replaceSchema(&$stub)
protected function replaceClass($stub, $name)
{
$stub = str_replace('{{class}}', $name, $stub);

return $stub;
}

Expand Down Expand Up @@ -174,4 +191,16 @@ protected function getArguments()
['tableTwo', InputArgument::REQUIRED, 'The name of the second table.']
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null]
];
}
}
16 changes: 16 additions & 0 deletions src/Migrations/SyntaxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function create($schema, $meta)
return compact('up', 'down');
}

public function createPivotSchema($schema, $stub)
{
$up = $this->createSchemaForPivot($schema,$stub);
return compact('up');
}

/**
* Create the schema for the "up" method.
*
Expand Down Expand Up @@ -58,6 +64,16 @@ private function createSchemaForUpMethod($schema, $meta)
throw new GeneratorException;
}

private function createSchemaForPivot($schema,$stub)
{
$fields = $this->constructSchema($schema);

return $this->insert($fields)->into($stub);

// Otherwise, we have no idea how to proceed.
throw new GeneratorException;
}

/**
* Construct the syntax for a down field.
*
Expand Down
34 changes: 34 additions & 0 deletions src/stubs/pivot-schema.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class {{class}} extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('{{pivotTableName}}', function (Blueprint $table) {
$table->integer('{{columnOne}}_id')->unsigned()->index();
$table->foreign('{{columnOne}}_id')->references('id')->on('{{tableOne}}')->onDelete('cascade');
$table->integer('{{columnTwo}}_id')->unsigned()->index();
$table->foreign('{{columnTwo}}_id')->references('id')->on('{{tableTwo}}')->onDelete('cascade');
{{schema_up}}
$table->primary(['{{columnOne}}_id', '{{columnTwo}}_id']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('{{pivotTableName}}');
}
}