<?php
 namespace App\Http\Controllers\Store; use App\Http\Controllers\Controller; use App\Models\BlogPost; use App\Models\Post; use App\Models\Tag; use App\Services\LocaleOverrideService; use Illuminate\Http\Request; use Illuminate\View\View; class BlogController extends Controller { public function index(Request $request): View { $tagSlug = $request->query('tag'); $query = BlogPost::query() ->where('status', 'published') ->where(fn ($q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now())) ->with(['tags', 'author']) ->orderBy('published_at', 'desc'); if ($tagSlug) { $query->whereHas('tags', fn ($q) => $q->where('slug', $tagSlug)); } $posts = $query->paginate(12)->withQueryString(); $featured = BlogPost::query() ->where('status', 'published') ->where('is_featured', true) ->where(fn ($q) => $q->whereNull('published_at')->orWhere('published_at', '<=', now())) ->with('author') ->orderBy('published_at', 'desc') ->limit(3) ->get(); $tags = Tag::whereHas('blogPosts')->orderBy('slug')->get(); return view('blog.index', compact('posts', 'featured', 'tags', 'tagSlug')); } public function show(string $slug, Request $request, LocaleOverrideService $localeService): View { $result = Post::findBySlugInAnyLocale($slug, 'blog_post', app()->getLocale()); if (! $result) { abort(404); } $post = $result['post']; $matchedLocale = $result['matched_locale']; if ($matchedLocale && $matchedLocale !== app()->getLocale()) { $localeService->resolveFromSlug($matchedLocale, $request); } $post->increment('view_count'); $tagIds = $post->tags->pluck('id'); $related = BlogPost::query() ->where('id', '!=', $post->id) ->where('status', 'published') ->when($tagIds->isNotEmpty(), fn ($q) => $q->whereHas('tags', fn ($qq) => $qq->whereIn('tags.id', $tagIds))) ->orderBy('published_at', 'desc') ->limit(4) ->get(); return view('blog.show', compact('post', 'related')); } } 