👁 查看:ArchiveController.php
路径:/home/boguraj1/public_html/epaper/app/Http/Controllers/ArchiveController.php
大小:1.24 KB · 修改:2026-06-25 22:25:44 · 权限:0644 · 可写
<?php

namespace App\Http\Controllers;

use App\Models\Newspaper;
use Illuminate\Http\Request;

class ArchiveController extends Controller
{
    public function index(Request $request)
    {
        $query = Newspaper::published()->orderByDesc('publish_date');

        if ($request->filled('month')) {
            $query->whereRaw("DATE_FORMAT(publish_date, '%Y-%m') = ?", [$request->month]);
        }

        if ($request->filled('year')) {
            $query->whereYear('publish_date', $request->year);
        }

        $newspapers = $query->paginate(20)->withQueryString();

        $years = Newspaper::published()
            ->selectRaw('YEAR(publish_date) as year')
            ->distinct()
            ->orderByDesc('year')
            ->pluck('year');

        $months = collect();
        if ($request->filled('year')) {
            $months = Newspaper::published()
                ->whereYear('publish_date', $request->year)
                ->selectRaw("DATE_FORMAT(publish_date, '%Y-%m') as month_key, DATE_FORMAT(publish_date, '%M %Y') as month_label")
                ->distinct()
                ->orderByDesc('month_key')
                ->get();
        }

        return view('frontend.archive', compact('newspapers', 'years', 'months'));
    }
}