Skip to content

Commit e94a682

Browse files
committed
chore: Use Pivot instead of Model for MountNode
1 parent 98d8510 commit e94a682

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

app/Models/MountNode.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/Models/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function isUnderMaintenance(): bool
241241

242242
public function mounts(): HasManyThrough
243243
{
244-
return $this->hasManyThrough(Mount::class, MountNode::class, 'node_id', 'id', 'id', 'mount_id');
244+
return $this->hasManyThrough(Mount::class, NodeMount::class, 'node_id', 'id', 'id', 'mount_id');
245245
}
246246

247247
/**

app/Models/NodeMount.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Relations\Pivot;
6+
7+
class NodeMount extends Pivot
8+
{
9+
protected $table = 'node_mount';
10+
11+
protected $primaryKey = null;
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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::table('mount_node', function (Blueprint $table) {
15+
$table->dropForeign(['mount_id']);
16+
});
17+
18+
Schema::rename('mount_node', 'node_mount');
19+
20+
Schema::table('mount_node', function (Blueprint $table) {
21+
$table->foreign('mount_id')
22+
->references('id')
23+
->on('node_mount')
24+
->cascadeOnDelete()
25+
->cascadeOnUpdate();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*/
32+
public function down(): void
33+
{
34+
35+
Schema::table('mount_node', function (Blueprint $table) {
36+
$table->dropForeign(['mount_id']);
37+
});
38+
39+
Schema::rename('node_mount', 'mount_node');
40+
41+
Schema::table('mount_node', function (Blueprint $table) {
42+
$table->foreign('mount_id')
43+
->references('id')
44+
->on('mount_node')
45+
->cascadeOnDelete()
46+
->cascadeOnUpdate();
47+
});
48+
}
49+
};

0 commit comments

Comments
 (0)