Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

pelican-dev/panel

1722 stars
4 code files
View pelican-dev/panel on GitHub

database/migrations/2025_05_01_193002_move_to_mountables.php

Open in GitHub
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
public function up(): void
{
Schema::create('mountables', function (Blueprint $table) {
$table->unsignedInteger('mount_id');
 
$table->string('mountable_type');
$table->unsignedBigInteger('mountable_id');
$table->index(['mountable_id', 'mountable_type'], 'mountables_mountable_id_mountable_type_index');
 
$table->foreign('mount_id')
->references('id') // mount id
->on('mounts')
->onDelete('cascade');
 
$table->primary(['mount_id', 'mountable_id', 'mountable_type'],
'mountables_mountable_type_primary');
});
 
Schema::table('mount_node', function (Blueprint $table) {
$table->dropForeign(['node_id']);
$table->dropForeign(['mount_id']);
$table->dropUnique(['node_id', 'mount_id']);
});
 
$inserts = [];
$nodeMounts = DB::table('mount_node')->get();
$nodeMounts->each(function ($mount) use (&$inserts) {
$inserts[] = [
'mount_id' => $mount->mount_id,
'mountable_type' => 'node',
'mountable_id' => $mount->node_id,
];
});
 
Schema::table('mount_server', function (Blueprint $table) {
$table->dropForeign(['server_id']);
$table->dropForeign(['mount_id']);
$table->dropUnique(['server_id', 'mount_id']);
});
 
$serverMounts = DB::table('mount_server')->get();
$serverMounts->each(function ($mount) use (&$inserts) {
$inserts[] = [
'mount_id' => $mount->mount_id,
'mountable_type' => 'server',
'mountable_id' => $mount->server_id,
];
});
 
Schema::table('egg_mount', function (Blueprint $table) {
$table->dropForeign(['egg_id']);
$table->dropForeign(['mount_id']);
$table->dropUnique(['egg_id', 'mount_id']);
});
 
$eggMounts = DB::table('egg_mount')->get();
$eggMounts->each(function ($mount) use (&$inserts) {
$inserts[] = [
'mount_id' => $mount->mount_id,
'mountable_type' => 'egg',
'mountable_id' => $mount->egg_id,
];
});
 
DB::transaction(function () use ($inserts) {
DB::table('mountables')->insert($inserts);
});
 
Schema::drop('mount_node');
Schema::drop('mount_server');
Schema::drop('egg_mount');
}
};

app/Models/Mount.php

Open in GitHub
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
class Mount extends Model implements Validatable
{
// ...
 
public function eggs(): MorphToMany
{
return $this->morphedByMany(Egg::class, 'mountable');
}
 
public function nodes(): MorphToMany
{
return $this->morphedByMany(Node::class, 'mountable');
}
 
public function servers(): MorphToMany
{
return $this->morphedByMany(Server::class, 'mountable');
}
}

app/Providers/AppServiceProvider.php

Open in GitHub
use App\Models;
use Illuminate\Database\Eloquent\Relations\Relation;
 
class AppServiceProvider extends ServiceProvider
{
public function boot(
Application $app,
SoftwareVersionService $versionService,
Repository $config,
): void {
// ...
 
Relation::enforceMorphMap([
'allocation' => Models\Allocation::class,
'api_key' => Models\ApiKey::class,
'backup' => Models\Backup::class,
'database' => Models\Database::class,
'egg' => Models\Egg::class,
'egg_variable' => Models\EggVariable::class,
'schedule' => Models\Schedule::class,
'server' => Models\Server::class,
'ssh_key' => Models\UserSSHKey::class,
'task' => Models\Task::class,
'user' => Models\User::class,
'node' => Models\Node::class,
]);
 
// ...
}
 
// ...
}

app/Models/Server.php

Open in GitHub
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
class Server extends Model implements Validatable
{
// ...
 
public function mounts(): MorphToMany
{
return $this->morphToMany(Mount::class, 'mountable');
}
 
// ...
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.