HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/sarvodayahospital/app/Http/Controllers/NewsController.php
<?php

namespace App\Http\Controllers;

use App\Models\CMSPage;
use App\Models\News;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\SEOMeta;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NewsController 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 = News::query()->has('newsTypeCollection');

        $query->when(
            $by_year > 0,
            function ($q) use ($by_year) {
                return $q->whereYear('PublicationDate', $by_year);
            }
        );

        if ($by_year == 0) {
            $query->when(
                $by_month > 0,
                function ($q) use ($by_month) {
                    return $q->whereMonth('PublicationDate', $by_month)
                        ->whereYear('PublicationDate', date("Y"));
                }
            );
        } else {
            $query->when(
                $by_month > 0,
                function ($q) use ($by_month) {
                    return $q->whereMonth('PublicationDate', $by_month);
                }
            );
        }

        // $query->when(
        //     $sort_by == 'A-Z', function ($q) {
        //         return $q->orderBy('Title', 'ASC');
        //     }
        // );
        // $query->when(
        //     $sort_by == 'Latest', function ($q) {
        //         return $q->orderBy('published_at', 'DESC');
        //     }
        // );
        // $query->when(
        //     $sort_by == 'Z-A', function ($q) {
        //         return $q->orderBy('Title', 'DESC');
        //     }
        // );

        $news = $query->orderBy('PublicationDate', 'DESC')->paginate(60)->appends(request()->query());

        $years = News::query()
            ->select(DB::raw('YEAR(PublicationDate) 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.news', compact('news', 'years', 'seo'));
    }
}