Skip to content

Commit 877b466

Browse files
Remove custom pivot table naming (#747)
1 parent 1a0a5bb commit 877b466

12 files changed

+157
-105
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function output(Tree $tree, $overwrite = false): array
6868
*/
6969
foreach ($tree->models() as $model) {
7070
$tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);
71+
7172
if (!empty($model->pivotTables())) {
7273
foreach ($model->pivotTables() as $pivotSegments) {
7374
$pivotTableName = $this->getPivotTableName($pivotSegments);
@@ -358,16 +359,7 @@ protected function disableForeignKeyConstraints($stub): string
358359

359360
protected function getPivotTableName(array $segments): string
360361
{
361-
$isCustom = collect($segments)
362-
->filter(fn ($segment) => Str::contains($segment, ':'))->first();
363-
364-
if ($isCustom) {
365-
$table = Str::after($isCustom, ':');
366-
367-
return $table;
368-
}
369-
370-
$segments = array_map(fn ($name) => Str::snake($name), $segments);
362+
$segments = array_map(fn ($name) => Str::of($name)->before(':')->snake()->value(), $segments);
371363
sort($segments);
372364

373365
return strtolower(implode('_', $segments));

src/Generators/ModelGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ protected function buildRelationships(Model $model): string
383383
$relationship .= sprintf('%s->withTimestamps()', PHP_EOL . str_pad(' ', 12));
384384
}
385385
} else {
386-
$relationship = sprintf('$this->%s(%s::class, \'%s\')', $type, $fqcn, $column_name);
386+
$relationship = sprintf('$this->%s(%s::class)', $type, $fqcn);
387387
}
388388
$column_name = $class;
389389
} else {
@@ -393,7 +393,7 @@ protected function buildRelationships(Model $model): string
393393
if ($type === 'morphTo') {
394394
$method_name = Str::lower($class_name);
395395
} elseif (in_array($type, ['hasMany', 'belongsToMany', 'morphMany', 'morphToMany', 'morphedByMany'])) {
396-
$method_name = Str::plural($is_model_fqn ? Str::afterLast($column_name, '\\') : $column_name);
396+
$method_name = Str::plural($is_pivot ? $column_name : $method_name);
397397
}
398398

399399
$relationship_type = 'Illuminate\\Database\\Eloquent\\Relations\\' . Str::studly($type === 'morphedByMany' ? 'morphToMany' : $type);

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,35 @@ public function using_ulids_output_also_creates_pivot_table_migration(): void
185185
$this->assertEquals(['created' => [$journey_model_migration, $diary_model_migration, $pivot_migration]], $this->subject->output($tree));
186186
}
187187

188+
#[Test]
189+
public function using_alias_output_also_aliases_pivot_table_migration(): void
190+
{
191+
$this->filesystem->expects('stub')
192+
->with('migration.stub')
193+
->andReturn($this->stub('migration.stub'));
194+
195+
$now = Carbon::now();
196+
Carbon::setTestNow($now);
197+
198+
$journey_model_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_customers_table.php');
199+
$diary_model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_pets_table.php');
200+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_customer_pet_table.php');
201+
202+
$this->filesystem->expects('exists')->times(3)->andReturn(false);
203+
204+
$this->filesystem->expects('put')
205+
->with($journey_model_migration, $this->fixture('migrations/belongs-to-many-alias-customers.php'));
206+
$this->filesystem->expects('put')
207+
->with($diary_model_migration, $this->fixture('migrations/belongs-to-many-alias-pets.php'));
208+
$this->filesystem->expects('put')
209+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-alias-customer-pet.php'));
210+
211+
$tokens = $this->blueprint->parse($this->fixture('drafts/belongs-to-many-using-alias.yaml'));
212+
$tree = $this->blueprint->analyze($tokens);
213+
214+
$this->assertEquals(['created' => [$journey_model_migration, $diary_model_migration, $pivot_migration]], $this->subject->output($tree));
215+
}
216+
188217
#[Test]
189218
public function output_also_creates_pivot_table_migration(): void
190219
{
@@ -367,32 +396,6 @@ public function output_does_not_duplicate_pivot_table_migration(): void
367396
$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
368397
}
369398

370-
#[Test]
371-
public function output_also_creates_pivot_table_migration_with_custom_name(): void
372-
{
373-
$this->filesystem->expects('stub')
374-
->with('migration.stub')
375-
->andReturn($this->stub('migration.stub'));
376-
377-
$now = Carbon::now();
378-
Carbon::setTestNow($now);
379-
380-
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
381-
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_test_table.php');
382-
383-
$this->filesystem->expects('exists')->twice()->andReturn(false);
384-
385-
$this->filesystem->expects('put')
386-
->with($model_migration, $this->fixture('migrations/custom-pivot-table-name-user.php'));
387-
$this->filesystem->expects('put')
388-
->with($pivot_migration, $this->fixture('migrations/custom-pivot-table-name-test.php'));
389-
390-
$tokens = $this->blueprint->parse($this->fixture('drafts/custom-pivot-table-name.yaml'));
391-
$tree = $this->blueprint->analyze($tokens);
392-
393-
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
394-
}
395-
396399
#[Test]
397400
public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix(): void
398401
{

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -526,34 +526,37 @@ public function output_generates_models_with_custom_namespace_correctly(): void
526526
}
527527

528528
#[Test]
529-
public function output_generates_models_with_custom_pivot_table_name(): void
529+
public function output_generates_models_with_alias_belongs_to_many(): void
530530
{
531531
$this->filesystem->expects('stub')
532532
->with('model.class.stub')
533533
->andReturn($this->stub('model.class.stub'));
534534
$this->filesystem->expects('stub')
535+
->twice()
535536
->with('model.fillable.stub')
536537
->andReturn($this->stub('model.fillable.stub'));
537538
$this->filesystem->expects('stub')
539+
->twice()
538540
->with('model.casts.stub')
539541
->andReturn($this->stub('model.casts.stub'));
540542
$this->filesystem->expects('stub')
543+
->twice()
541544
->with('model.method.stub')
542545
->andReturn($this->stub('model.method.stub'));
543-
$this->filesystem->expects('stub')
544-
->with('model.hidden.stub')
545-
->andReturn($this->stub('model.hidden.stub'));
546546

547547
$this->filesystem->expects('exists')
548+
->twice()
548549
->with('app/Models')
549550
->andReturnTrue();
550551
$this->filesystem->expects('put')
551-
->with('app/Models/User.php', $this->fixture('models/custom-pivot-table-name.php'));
552+
->with('app/Models/Pet.php', $this->fixture('models/belongs-to-many-alias-pet.php'));
553+
$this->filesystem->expects('put')
554+
->with('app/Models/Customer.php', $this->fixture('models/belongs-to-many-alias-customer.php'));
552555

553-
$tokens = $this->blueprint->parse($this->fixture('drafts/custom-pivot-table-name.yaml'));
556+
$tokens = $this->blueprint->parse($this->fixture('drafts/belongs-to-many-using-alias.yaml'));
554557
$tree = $this->blueprint->analyze($tokens);
555558

556-
$this->assertEquals(['created' => ['app/Models/User.php']], $this->subject->output($tree));
559+
$this->assertEquals(['created' => ['app/Models/Customer.php', 'app/Models/Pet.php']], $this->subject->output($tree));
557560
}
558561

559562
#[Test]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Customer:
3+
name: string
4+
relationships:
5+
belongsToMany: Pet
6+
7+
Pet:
8+
name: string
9+
relationships:
10+
belongsToMany: Customer:owner

tests/fixtures/drafts/custom-pivot-table-name.yaml

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

tests/fixtures/migrations/custom-pivot-table-name-user.php renamed to tests/fixtures/migrations/belongs-to-many-alias-customers.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
*/
1212
public function up(): void
1313
{
14-
Schema::create('users', function (Blueprint $table) {
14+
Schema::create('customers', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('name');
17-
$table->rememberToken();
1817
$table->timestamps();
1918
});
2019
}
@@ -24,6 +23,6 @@ public function up(): void
2423
*/
2524
public function down(): void
2625
{
27-
Schema::dropIfExists('users');
26+
Schema::dropIfExists('customers');
2827
}
2928
};

tests/fixtures/migrations/custom-pivot-table-name-test.php renamed to tests/fixtures/migrations/belongs-to-many-alias-pets.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
*/
1212
public function up(): void
1313
{
14-
Schema::create('test', function (Blueprint $table) {
15-
$table->foreignId('account_id');
16-
$table->foreignId('user_id');
14+
Schema::create('pets', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('name');
17+
$table->timestamps();
1718
});
1819
}
1920

@@ -22,6 +23,6 @@ public function up(): void
2223
*/
2324
public function down(): void
2425
{
25-
Schema::dropIfExists('test');
26+
Schema::dropIfExists('pets');
2627
}
2728
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
9+
class Customer extends Model
10+
{
11+
use HasFactory;
12+
13+
/**
14+
* The attributes that are mass assignable.
15+
*
16+
* @var array
17+
*/
18+
protected $fillable = [
19+
'name',
20+
];
21+
22+
/**
23+
* The attributes that should be cast to native types.
24+
*
25+
* @var array
26+
*/
27+
protected $casts = [
28+
'id' => 'integer',
29+
];
30+
31+
public function pets(): BelongsToMany
32+
{
33+
return $this->belongsToMany(Pet::class);
34+
}
35+
}

0 commit comments

Comments
 (0)