From 9ce024d4796fb257d1b7eaab44b67dff66cfd710 Mon Sep 17 00:00:00 2001 From: Harry Bayliss Date: Thu, 22 May 2025 17:47:55 +0100 Subject: [PATCH] deployments relationship --- app/Models/Server.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Models/Server.php b/app/Models/Server.php index a3e4268..dc43701 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Spatie\Ssh\Ssh; class Server extends Model @@ -35,7 +36,7 @@ class Server extends Model $server->internal_ip_ending = $existingServer ? $existingServer->internal_ip_ending + 1 : 2; - $server->internal_ip = config('keystone.internal_ip_base').$server->internal_ip_ending; + $server->internal_ip = config('keystone.internal_ip_base') . $server->internal_ip_ending; }); } @@ -69,6 +70,26 @@ class Server extends Model return $this->belongsTo(Provider::class); } + public function serviceDeployments(): HasManyThrough + { + return $this->hasManyThrough( + Deployment::class, + Service::class, + 'server_id', + 'target_id', + )->where('target_type', (new Service)->getMorphClass()); + } + + public function environmentDeployments(): HasManyThrough + { + return $this->hasManyThrough( + Deployment::class, + Environment::class, + 'server_id', + 'target_id', + )->where('target_type', (new Environment)->getMorphClass()); + } + public function sshClient(string $user = 'root'): Ssh { return Ssh::create($user, $this->ipv4)