Skip to content

Commit 42e6409

Browse files
Merge pull request #2 from RajTechnologiesPvtLtd/feature/pivot-table
create a pivot table generator
2 parents c64781c + 127fdeb commit 42e6409

File tree

5 files changed

+226
-3
lines changed

5 files changed

+226
-3
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ class RepositoryServiceProvider extends ServiceProvider
175175
}
176176
}
177177
```
178+
# Pivot Table Generator
179+
180+
## Usage
181+
Run the following command on the command-line to generate a new migration for the pivot table.
182+
```
183+
php artisan make:pivot {first_table_name} {second_table_name}
184+
```
185+
186+
The command will create a new migration in ```database/migrations```. Run the migrations to create the table.
187+
```
188+
php artisan migrate
189+
```
178190

179191
# Service Generator
180192

@@ -348,8 +360,6 @@ class PostController extends Controller
348360
}
349361
```
350362

351-
352-
353363
## Contributing
354364

355365
- [Bhargav Raviya](https://github.com/bhargavraviya)

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"generator",
1414
"cli",
1515
"artisan",
16+
"pivot",
17+
"migrations"
1618
"services",
1719
"pattern"
1820
],

src/Console/MakePivotCommand.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace RajTechnologies\Tools\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7+
use Illuminate\Support\Str;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
class MakePivotCommand extends GeneratorCommand
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'make:pivot {first_table_name} {second_table_name}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create a new pivot table';
25+
26+
/**
27+
* The type of class being generated.
28+
*
29+
* @var string
30+
*/
31+
protected $type = 'Migration';
32+
33+
/**
34+
* Build the class with the given name.
35+
*
36+
* @param string $name
37+
* @return string
38+
*
39+
* @throws FileNotFoundException
40+
*/
41+
protected function buildClass($name): string
42+
{
43+
$stub = $this->files->get($this->getStub());
44+
45+
return $this->populateStub($stub);
46+
}
47+
48+
/**
49+
* Replace the namespace for the given stub.
50+
*
51+
* @param string $stub
52+
* @return string
53+
*/
54+
protected function populateStub(string $stub): string
55+
{
56+
list($firstTable, $secondTable) = $this->getSortedInput();
57+
58+
$searches = [
59+
[
60+
'{{ first_table_name }}',
61+
'{{ second_table_name }}',
62+
'{{ class }}',
63+
'{{ pivot_table }}'
64+
],
65+
];
66+
67+
foreach ($searches as $search) {
68+
$stub = str_replace(
69+
$search,
70+
[
71+
$firstTable,
72+
$secondTable,
73+
$this->getClassName(),
74+
$this->getNameInput()
75+
],
76+
$stub
77+
);
78+
}
79+
80+
return $stub;
81+
}
82+
83+
/**
84+
* @return string
85+
*/
86+
public function getClassName(): string
87+
{
88+
$name = Str::studly($this->getNameInput());
89+
90+
return "Create{$name}Table";
91+
}
92+
93+
/**
94+
* @return array
95+
*/
96+
protected function getSortedInput(): array
97+
{
98+
$inputValues = [
99+
$this->getSingularInput('first_table_name'),
100+
$this->getSingularInput('second_table_name')
101+
];
102+
sort($inputValues);
103+
104+
return $inputValues;
105+
}
106+
107+
/**
108+
* Get the desired class name from the input.
109+
*
110+
* @return string
111+
*/
112+
protected function getNameInput(): string
113+
{
114+
$sortedInput = $this->getSortedInput();
115+
116+
return implode('_', $sortedInput);
117+
}
118+
119+
/**
120+
* @param string $name
121+
* @return string
122+
*/
123+
protected function getSingularInput(string $name): string
124+
{
125+
return Str::lower(Str::singular(trim($this->argument($name))));
126+
}
127+
128+
/**
129+
* Get the stub file for the generator.
130+
*
131+
* @return string
132+
*/
133+
protected function getStub(): string
134+
{
135+
return __DIR__ . '/../../stubs/pivot_migration.stub';
136+
}
137+
138+
/**
139+
* Get the default namespace for the class.
140+
*
141+
* @param string $rootNamespace
142+
* @return string
143+
*/
144+
protected function getDefaultNamespace($rootNamespace): string
145+
{
146+
return $rootNamespace . '/../database/migrations';
147+
}
148+
149+
/**
150+
* Get the console command arguments.
151+
*
152+
* @return array
153+
*/
154+
protected function getArguments(): array
155+
{
156+
return [
157+
['first_table_name', InputArgument::REQUIRED, 'The name of the first table'],
158+
['second_table_name', InputArgument::REQUIRED, 'The name of the second table'],
159+
];
160+
}
161+
162+
/**
163+
* Get the destination class path.
164+
*
165+
* @param string $name
166+
* @return string
167+
*/
168+
protected function getPath($name): string
169+
{
170+
$filename = now()->format('Y_m_d_his') . "_create_" . $this->getNameInput() . "_table.php";
171+
172+
return database_path('migrations/' . $filename);
173+
}
174+
}

src/ToolServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\File;
88
use RajTechnologies\Tools\Console\MakeRepository;
99
use RajTechnologies\Tools\Console\MakeRepositoryInterface;
10+
use RajTechnologies\Tools\Console\MakePivotCommand;
1011
use RajTechnologies\Tools\Console\MakeModel;
1112
use RajTechnologies\Tools\Console\MakeService;
1213

@@ -30,6 +31,11 @@ public function boot(){
3031
], 'config');
3132
}
3233
// Repository Pattern End
34+
// Pivot Table Start
35+
if ($this->app->runningInConsole()) {
36+
$this->commands([MakePivotCommand::class]);
37+
}
38+
// Pivot Table End
3339
// Service Start
3440
if ($this->app->runningInConsole()) {
3541
$this->commands([
@@ -72,4 +78,4 @@ protected function bindInterfaces()
7278
// Repository Pattern End
7379
}
7480

75-
?>
81+
?>

stubs/pivot_migration.stub

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class {{ class }} extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('{{ pivot_table }}', function (Blueprint $table) {
17+
$table->foreignId('{{ first_table_name }}_id')->onDelete('cascade');
18+
$table->foreignId('{{ second_table_name }}_id')->onDelete('cascade');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('{{ pivot_table }}');
30+
}
31+
}

0 commit comments

Comments
 (0)