PlatformEnum::class, 'settings' => 'array', 'is_active' => 'boolean', 'last_tested_at' => 'datetime' ]; // Encrypt password when storing protected function password(): Attribute { return Attribute::make( get: fn ($value) => $value ? Crypt::decryptString($value) : null, set: fn ($value) => $value ? Crypt::encryptString($value) : null, ); } // Encrypt API token when storing protected function apiToken(): Attribute { return Attribute::make( get: fn ($value) => $value ? Crypt::decryptString($value) : null, set: fn ($value) => $value ? Crypt::encryptString($value) : null, ); } // Get the active accounts for a platform (returns collection) public static function getActive(PlatformEnum $platform): Collection { return static::where('platform', $platform) ->where('is_active', true) ->get(); } // Set this account as active (deactivates others for same platform) public function setAsActive(): void { // Deactivate other accounts for this platform static::where('platform', $this->platform) ->where('id', '!=', $this->id) ->update(['is_active' => false]); // Activate this account $this->update(['is_active' => true]); } public function channels(): BelongsToMany { return $this->belongsToMany(PlatformChannel::class, 'platform_account_channels') ->withPivot(['is_active', 'priority']) ->withTimestamps(); } public function activeChannels(): BelongsToMany { return $this->channels() ->wherePivot('is_active', true) ->orderByPivot('priority', 'desc'); } }