File: /var/www/html/sarvodayahospital/app/Http/Controllers/AwardController.php
<?php
namespace App\Http\Controllers;
use App\Models\Award;
use App\Models\CMSPage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class AwardController extends Controller
{
public function index(Request $request)
{
$sort_by = $request->filled('sortby') ? $request->get('sortby') : 'id';
$by_year = $request->filled('year') ? (int) $request->get('year') : 0;
$by_month = $request->filled('month') ? (int) $request->get('month') : null;
$seo = [];
$query = Award::query();
$query->when(
$by_year > 0,
function ($q) use ($by_year) {
return $q->whereYear('Date', $by_year);
}
);
if ($by_year == 0) {
$query->when(
$by_month > 0,
function ($q) use ($by_month) {
return $q->whereMonth('Date', $by_month)
->whereYear('Date', date("Y"));
}
);
} else {
$query->when(
$by_month > 0,
function ($q) use ($by_month) {
return $q->whereMonth('Date', $by_month);
}
);
}
$awards = $query->select('id', 'AwardName', 'Slug', 'Date')
->orderby('Date', 'DESC')
->paginate(20);
$years = Award::query()
->select(DB::raw('YEAR(Date) as publicationYear'))
->groupBy('publicationYear')
->orderBy('publicationYear', 'DESC')
->get();
$cmsPage = CMSPage::query()
->where('Slug', request()->path())
->firstOrFail();
if ($cmsPage) {
$seo = $cmsPage->seoComponent()->first();
}
return view('pages.award', compact('awards', 'years', 'seo'));
}
public function show($slug)
{
$seo = [];
$award = Award::query()
->where('Slug', $slug)
->firstOrFail();
if ($award) {
$seo = $award->seoComponent()->first();
}
$awards = $awards = Award::query()
->where('Slug', '!=', $slug)
->select('id', 'AwardName', 'Slug', 'Date')
->take(6)
->get();
return view('pages.single-award', compact('award', 'awards', 'seo'));
}
}