From d7703efcdc53c8b915b5cf3f42212e2e34557d75 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 2 Dec 2024 13:12:25 +0100 Subject: [PATCH] optimize db query --- .../Project/Application/Configuration.php | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/app/Livewire/Project/Application/Configuration.php b/app/Livewire/Project/Application/Configuration.php index 5e7f83772..cce3bdd39 100644 --- a/app/Livewire/Project/Application/Configuration.php +++ b/app/Livewire/Project/Application/Configuration.php @@ -16,29 +16,24 @@ class Configuration extends Component public function mount() { - $project = currentTeam() - ->projects() - ->select('id', 'uuid', 'team_id') - ->where('uuid', request()->route('project_uuid')) - ->firstOrFail(); - - $environment = $project->environments() - ->select('id', 'name', 'project_id') - ->where('name', request()->route('environment_name')) - ->firstOrFail(); - - $application = $environment->applications() - ->with(['destination']) + $this->application = Application::query() + ->whereHas('environment.project', function ($query) { + $query->where('team_id', currentTeam()->id) + ->where('uuid', request()->route('project_uuid')); + }) + ->whereHas('environment', function ($query) { + $query->where('name', request()->route('environment_name')); + }) ->where('uuid', request()->route('application_uuid')) + ->with(['destination' => function ($query) { + $query->select('id', 'server_id'); + }]) ->firstOrFail(); - $this->application = $application; - - if ($application->destination && $application->destination->server) { - $mainServer = $application->destination->server; + if ($this->application->destination && $this->application->destination->server_id) { $this->servers = Server::ownedByCurrentTeam() ->select('id', 'name') - ->where('id', '!=', $mainServer->id) + ->where('id', '!=', $this->application->destination->server_id) ->get(); } else { $this->servers = collect();