organisation_id === (int) $organisation->id && (int) $environment->application_id === (int) $application->id, 404, ); $environment->loadMissing('services.replicas'); if ($organisation->registries()->doesntExist() && $this->serverIdsFor($environment)->count() > 1) { return back()->with('error', 'Configure a registry before deploying this environment to multiple servers.'); } dispatch(new DeployEnvironment( environment: $environment, targetCommit: $request->validated('target_commit') ?: null, )); return redirect()->route('environments.show', [ 'organisation' => $organisation->id, 'application' => $application->id, 'environment' => $environment->id, ]); } /** * @return \Illuminate\Support\Collection */ private function serverIdsFor(Environment $environment): Collection { return $environment->services ->flatMap(fn ($service) => [ $service->server_id, ...$service->replicas->pluck('server_id')->all(), ]) ->filter() ->unique() ->values(); } }