'boolean', 'sort_order' => 'integer', 'clicks' => 'integer', ]; /** * Boot-Methode für automatische Short-Code-Generierung */ protected static function boot() { parent::boot(); static::creating(function ($model) { // Short-Code nur generieren wenn URL vorhanden ist if ($model->url && empty($model->short_code)) { $model->short_code = self::generateUniqueShortCode(); } }); } /** * Generiert einen eindeutigen Short-Code */ public static function generateUniqueShortCode(): string { do { // Generiere einen 6-stelligen alphanumerischen Code $code = strtolower(substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, 6)); } while (self::where('short_code', $code)->exists()); return $code; } /** * Gibt die Short-URL zurück (dynamisch je nach Umgebung) */ public function getShortUrlAttribute(): string { $basePath = config('display.base_path', '_display-b2in-eu'); $subdomain = config('display.subdomain'); $domain = config('display.domain', 'b2in.eu'); // Wenn Subdomain konfiguriert ist, verwende diese (Live-Server) if ($subdomain) { // Nutze HTTPS für Produktion $protocol = config('app.env') === 'production' ? 'https' : 'http'; return "{$protocol}://{$subdomain}.{$domain}/go.php?z={$this->short_code}"; } // Ansonsten verwende APP_URL + Base-Path (Testserver) $baseUrl = rtrim(config('app.url'), '/'); return "{$baseUrl}/{$basePath}/go.php?z={$this->short_code}"; } /** * Erhöht den Klick-Zähler */ public function incrementClicks(): void { $this->increment('clicks'); } /** * Scope für aktive Footer-Inhalte in sortierter Reihenfolge */ public function scopeActive($query) { return $query->where('is_active', true)->orderBy('sort_order'); } }