*/ public function build(WebPortalContext $context, string $slug): array { $portalValues = $context->portalValues; $language = $context->language; $company = Company::query() ->withoutGlobalScope(PortalScope::class) ->whereIn('portal', $portalValues) ->where('is_active', true) ->where('slug', $slug) ->with(['contacts']) ->first(); if (! $company) { abort(404); } $base = static fn (): Builder => PressRelease::query() ->withoutGlobalScope(PortalScope::class) ->where('company_id', $company->id) ->forPortalValues($portalValues) ->forLanguage($language) ->visibleInArchive(); // Keine öffentliche Newsroom-Seite ohne veröffentlichte Meldung. if (! $base()->exists()) { abort(404); } $latest = $base()->max('published_at'); $referenceNow = $latest ? Carbon::parse($latest) : now(); if ($referenceNow->gt(now())) { $referenceNow = now(); } $recentSince = $referenceNow->copy()->subDays(30); $oldest = $base()->min('published_at'); $with = [ 'company', 'category.translations' => fn ($query) => $query->where('locale', $language), 'images' => fn ($query) => $query->orderByDesc('is_preview')->orderBy('sort_order')->limit(1), ]; $leadRelease = $base()->with($with)->orderByDesc('published_at')->first(); $releases = $base() ->with($with) ->when($leadRelease, fn (Builder $query) => $query->whereKeyNot($leadRelease->getKey())) ->orderByDesc('published_at') ->paginate(10) ->withQueryString(); $mostReadReleases = $base() ->orderByDesc('hits') ->orderByDesc('published_at') ->limit(5) ->get(['id', 'slug', 'title', 'hits', 'portal', 'language']); $companyFilter = static function (Builder $query) use ($company, $portalValues, $language): void { $query ->withoutGlobalScope(PortalScope::class) ->where('company_id', $company->id) ->forPortalValues($portalValues) ->forLanguage($language) ->published(); }; $categories = Category::query() ->whereIn('portal', $portalValues) ->whereHas('pressReleases', fn (Builder $query) => $companyFilter($query)) ->with(['translations' => fn ($query) => $query->where('locale', $language)]) ->withCount(['pressReleases as releases_count' => fn (Builder $query) => $companyFilter($query)]) ->orderByDesc('releases_count') ->limit(8) ->get() ->map(function (Category $category): ?array { $translation = $category->translations->first(); if (! $translation) { return null; } return [ 'name' => $translation->name, 'href' => $translation->slug ? route('kategorie', ['slug' => $translation->slug]) : route('web.home'), 'count' => (int) ($category->releases_count ?? 0), ]; }) ->filter() ->values(); return [ 'company' => $company, 'referenceNow' => $referenceNow, 'leadRelease' => $leadRelease, 'releases' => $releases, 'mostReadReleases' => $mostReadReleases, 'categories' => $categories, 'newsroomStats' => [ 'total' => $base()->count(), 'recent' => $base()->where('published_at', '>=', $recentSince)->count(), 'today' => $base()->whereDate('published_at', $referenceNow->toDateString())->count(), 'totalHits' => (int) $base()->sum('hits'), 'sinceYear' => $oldest ? (int) Carbon::parse($oldest)->format('Y') : null, ], ]; } }