'datetime', ]; /** * The profile this version belongs to. */ public function profile(): BelongsTo { return $this->belongsTo(ConfigProfile::class, 'profile_id'); } /** * Workspace this version is for (null = system). * * Requires Core\Tenant module to be installed. */ public function workspace(): BelongsTo { if (class_exists(Workspace::class)) { return $this->belongsTo(Workspace::class); } // Return a null relationship when Tenant module is not installed return $this->belongsTo(self::class, 'workspace_id')->whereRaw('1 = 0'); } /** * Get the parsed snapshot data. * * @return array */ public function getSnapshotData(): array { return json_decode($this->snapshot, true) ?? []; } /** * Get the config values from the snapshot. * * @return array */ public function getValues(): array { $data = $this->getSnapshotData(); return $data['values'] ?? []; } /** * Get a specific value from the snapshot. * * @param string $key Config key code * @return mixed|null The value or null if not found */ public function getValue(string $key): mixed { $values = $this->getValues(); foreach ($values as $value) { if ($value['key'] === $key) { return $value['value']; } } return null; } /** * Check if a key exists in the snapshot. * * @param string $key Config key code */ public function hasKey(string $key): bool { $values = $this->getValues(); foreach ($values as $value) { if ($value['key'] === $key) { return true; } } return false; } /** * Get versions for a scope. * * @param int|null $workspaceId Workspace ID or null for system * @return Collection */ public static function forScope(?int $workspaceId = null): Collection { return static::where('workspace_id', $workspaceId) ->orderByDesc('created_at') ->get(); } /** * Get the latest version for a scope. * * @param int|null $workspaceId Workspace ID or null for system */ public static function latest(?int $workspaceId = null): ?self { return static::where('workspace_id', $workspaceId) ->orderByDesc('created_at') ->first(); } /** * Get versions created by a specific author. * * @return Collection */ public static function byAuthor(string $author): Collection { return static::where('author', $author) ->orderByDesc('created_at') ->get(); } /** * Get versions created within a date range. * * @param Carbon $from Start date * @param Carbon $to End date * @return Collection */ public static function inDateRange(Carbon $from, Carbon $to): Collection { return static::whereBetween('created_at', [$from, $to]) ->orderByDesc('created_at') ->get(); } }