network = new Cuid2; $this->servers = Server::isUsable()->get(); if ($server_id) { $this->selectedServer = $this->servers->find($server_id) ?: $this->servers->first(); $this->serverId = $this->selectedServer->id; } else { $this->selectedServer = $this->servers->first(); $this->serverId = $this->selectedServer->id; } $this->generateName(); } public function updatedServerId() { $this->selectedServer = $this->servers->find($this->serverId); $this->generateName(); } public function generateName() { $name = data_get($this->selectedServer, 'name', new Cuid2); $this->name = str("{$name}-{$this->network}")->kebab(); } public function submit() { try { $this->validate(); if ($this->isSwarm) { $found = $this->selectedServer->swarmDockers()->where('network', $this->network)->first(); if ($found) { throw new \Exception('Network already added to this server.'); } else { $docker = SwarmDocker::create([ 'name' => $this->name, 'network' => $this->network, 'server_id' => $this->selectedServer->id, ]); } } else { $found = $this->selectedServer->standaloneDockers()->where('network', $this->network)->first(); if ($found) { throw new \Exception('Network already added to this server.'); } else { $docker = StandaloneDocker::create([ 'name' => $this->name, 'network' => $this->network, 'server_id' => $this->selectedServer->id, ]); } } $this->redirect(route('destination.show', $docker->uuid)); } catch (\Throwable $e) { return handleError($e, $this); } } }