join('artworks as a', 'f.artwork_id', '=', 'a.id') ->where('a.is_approved', true) ->where('a.is_public', true) ->whereNull('a.deleted_at') ->whereRaw('MONTH(f.post_date) = ?', [$today->month]) ->whereRaw('DAY(f.post_date) = ?', [$today->day]) ->select('a.id', 'a.title as name', 'a.slug', 'a.hash', 'a.thumb_ext', DB::raw('f.post_date as featured_date')) ->orderBy('f.post_date', 'desc') ->paginate($perPage); } catch (\Throwable $e) { $artworks = null; } } // ── Strategy 2: new artwork_features table ─────────────────────────────── if (!$artworks || $artworks->total() === 0) { try { $artworks = DB::table('artwork_features as f') ->join('artworks as a', 'f.artwork_id', '=', 'a.id') ->where('f.is_active', true) ->where('a.is_approved', true) ->where('a.is_public', true) ->whereNull('a.deleted_at') ->whereNotNull('a.published_at') ->whereRaw('MONTH(f.featured_at) = ?', [$today->month]) ->whereRaw('DAY(f.featured_at) = ?', [$today->day]) ->select('a.id', 'a.title as name', 'a.slug', 'a.hash', 'a.thumb_ext', DB::raw('f.featured_at as featured_date')) ->orderBy('f.featured_at', 'desc') ->paginate($perPage); } catch (\Throwable $e) { $artworks = null; } } // ── Enrich with CDN thumbnails (batch load to avoid N+1) ───────────────── if ($artworks && method_exists($artworks, 'getCollection') && $artworks->count() > 0) { $ids = $artworks->getCollection()->pluck('id')->all(); $modelsById = Artwork::whereIn('id', $ids)->get()->keyBy('id'); $artworks->getCollection()->transform(function ($row) use ($modelsById) { /** @var ?Artwork $art */ $art = $modelsById->get($row->id); if ($art) { $row->thumb_url = $art->thumbUrl('md') ?? 'https://files.skinbase.org/default/missing_md.webp'; $row->art_url = '/art/' . $art->id . '/' . $art->slug; $row->name = $art->title ?: ($row->name ?? 'Untitled'); } else { $row->thumb_url = 'https://files.skinbase.org/default/missing_md.webp'; $row->art_url = '/art/' . $row->id; $row->name = $row->name ?? 'Untitled'; } return $row; }); } return view('legacy::today-in-history', [ 'artworks' => $artworks, 'page_title' => 'Popular on this day in history', 'todayLabel' => $today->format('F j'), ]); } }