diff --git a/app/Filament/Admin/Resources/Servers/Pages/EditServer.php b/app/Filament/Admin/Resources/Servers/Pages/EditServer.php index 43750626a..7dc164adf 100644 --- a/app/Filament/Admin/Resources/Servers/Pages/EditServer.php +++ b/app/Filament/Admin/Resources/Servers/Pages/EditServer.php @@ -14,7 +14,6 @@ use App\Models\Allocation; use App\Models\Egg; use App\Models\Node; use App\Models\Server; -use App\Models\ServerVariable; use App\Models\User; use App\Repositories\Daemon\DaemonServerRepository; use App\Services\Eggs\EggChangerService; @@ -637,14 +636,7 @@ class EditServer extends EditRecord /** @var Server $server */ $server = $this->getRecord(); - foreach ($server->variables as $variable) { - ServerVariable::query()->firstOrCreate([ - 'server_id' => $server->id, - 'variable_id' => $variable->id, - ], [ - 'variable_value' => $variable->server_value ?? '', - ]); - } + $server->ensureVariablesExist(); return $query->orderByPowerJoins('variable.sort'); }) diff --git a/app/Filament/Server/Pages/Startup.php b/app/Filament/Server/Pages/Startup.php index 42e261bf9..6464eb175 100644 --- a/app/Filament/Server/Pages/Startup.php +++ b/app/Filament/Server/Pages/Startup.php @@ -102,7 +102,11 @@ class Startup extends ServerFormPage ->schema([ Repeater::make('server_variables') ->hiddenLabel() - ->relationship('serverVariables', fn (Builder $query) => $query->where('egg_variables.user_viewable', true)->orderByPowerJoins('variable.sort')) + ->relationship('serverVariables', function (Builder $query, Server $server) { + $server->ensureVariablesExist(); + + return $query->where('egg_variables.user_viewable', true)->orderByPowerJoins('variable.sort'); + }) ->grid() ->disabled(fn (Server $server) => !auth()->user()->can(Permission::ACTION_STARTUP_UPDATE, $server)) ->reorderable(false)->addable(false)->deletable(false) diff --git a/app/Models/Server.php b/app/Models/Server.php index f3f3c0e00..098958c99 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -325,6 +325,18 @@ class Server extends Model implements Validatable return $this->hasMany(ServerVariable::class); } + public function ensureVariablesExist(): void + { + foreach ($this->eggVariables as $variable) { + ServerVariable::firstOrCreate([ + 'server_id' => $this->id, + 'variable_id' => $variable->id, + ], [ + 'variable_value' => $variable->default_value, + ]); + } + } + /** * Gets information for the node associated with this server. */