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/FindDoctorController.php
<?php

namespace App\Http\Controllers;

use App\Models\CMSPage;
use App\Models\Doctor;
use App\Models\Hospital;
use App\Models\Speciality;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class FindDoctorController extends Controller
{
    public function makeAnAppointment(Request $request)
    {
        $seo = [];
        $filterDoctors = new Collection();

        //================================================================
        //PLEASE DO NOT REMOVE THIS CODE FOR FUTURE IMPLIMENT ONLY
        //================================================================

        // if (($request->filled('doctor')) || ($request->filled('speciality')) || ($request->filled('hospital'))) {
        //     $doctors = !is_null($request->doctor) ? explode(',', $request->doctor) : null;
        //     $specialities = !is_null($request->speciality) ? explode(',', $request->speciality) : null;
        //     $hospital = !is_null($request->hospital) ? explode(',', $request->hospital) : null;

        //     if ($specialities) {
        //         $sp = [];
        //         $spo = Speciality::whereIn('Slug', $specialities)->with('ss')->get();
        //         foreach ($spo as $p) {
        //             $sp[] = $p->Slug;
        //             foreach ($p->ss as $s) {
        //                 $sp[] = $s->Slug;
        //             }
        //         }
        //     }
        //     if ($doctors) {
        //         $doc = Doctor::query()->WhereIn('Slug', $doctors)->get();
        //         //dd($doc);
        //         $filterDoctors = $filterDoctors->merge($doc);
        //     }

        //     $filter = Doctor::query();
        //     if ($specialities) {
        //         $filter->WhereHas('specialities', function ($q) use ($sp) {
        //             $q->WhereIn('Slug', $sp);
        //         });
        //         if ($hospital) {
        //             $filter->WhereHas('hospitals', function ($q) use ($hospital) {
        //                 $q->whereIn('hospitals.Slug', $hospital);
        //             });
        //         }
        //         $filterDoctors = $filterDoctors->merge($filter->get());
        //     } else {
        //         if ($hospital) {
        //             $filter->WhereHas('hospitals', function ($q) use ($hospital) {
        //                 $q->whereIn('hospitals.Slug', $hospital);
        //             });
        //             $filterDoctors = $filterDoctors->merge($filter->get());
        //         }
        //     }
        // }

        if (($request->filled('doctor')) || ($request->filled('speciality')) || ($request->filled('hospital'))) {
            $doctor = !is_null($request->doctor) ? $request->doctor : null;
            $specialities = !is_null($request->speciality) ? $request->speciality : null;
            $hospital = !is_null($request->hospital) ? explode(',', $request->hospital) : null;

            if ($specialities) {
                $sp = [];
                $spo = Speciality::where('Slug', $specialities)->with('ss')->get();
                foreach ($spo as $p) {
                    $sp[] = $p->Slug;
                    foreach ($p->ss as $s) {
                        $sp[] = $s->Slug;
                    }
                }
            }

            $filter = Doctor::query()->orderByRaw('ISNULL(Weight), Weight ASC')->orderBy('Surname', 'asc');
            if ($hospital) {
                if ($doctor) {
                    $doc = Doctor::query()->Where('Slug', $doctor)
                        ->WhereHas('hospitals', function ($q) use ($hospital) {
                            $q->whereIn('hospitals.Slug', $hospital);
                        })
                        ->orderByRaw('ISNULL(Weight), Weight ASC')
                        ->orderBy('Surname', 'asc')
                        ->get();
                    $filterDoctors = $filterDoctors->merge($doc);
                    if ($specialities) {
                        $filter->WhereHas('hospitals', function ($q) use ($hospital) {
                            $q->whereIn('hospitals.Slug', $hospital);
                        });

                        $filter->WhereHas('specialities', function ($q) use ($sp) {
                            $q->WhereIn('Slug', $sp);
                        });
                        $filterDoctors = $filterDoctors->merge($filter->get());
                    }
                } else {
                    if ($specialities) {
                        $filter->WhereHas('hospitals', function ($q) use ($hospital) {
                            $q->whereIn('hospitals.Slug', $hospital);
                        });

                        $filter->WhereHas('specialities', function ($q) use ($sp) {
                            $q->WhereIn('Slug', $sp);
                        });
                        $filterDoctors = $filterDoctors->merge($filter->get());
                    } else {
                        $filter->WhereHas('hospitals', function ($q) use ($hospital) {
                            $q->whereIn('hospitals.Slug', $hospital);
                        });
                        $filterDoctors = $filterDoctors->merge($filter->get());
                    }
                }
            } else {
                if ($doctor) {
                    $doc = Doctor::query()->Where('Slug', $doctor)
                        ->orderByRaw('ISNULL(Weight), Weight ASC')
                        ->orderBy('Surname', 'asc')
                        ->get();
                    $filterDoctors = $filterDoctors->merge($doc);
                }
                if ($specialities) {
                    $filter->WhereHas('specialities', function ($q) use ($sp) {
                        $q->WhereIn('Slug', $sp);
                    });
                    $filterDoctors = $filterDoctors->merge($filter->get());
                }
            }
        }

        $hospitals = Hospital::query()
            ->orderBy('Weight', 'asc')
            ->whereNotIn('Slug', ['sarvodaya-imaging-centre-with-nrch', 'sarvodaya-imaging-centre-with-ndmc'])
            ->get();

        $cmsPage = CMSPage::query()
            ->where('Slug', request()->path())
            ->firstOrFail();

        if ($cmsPage) {
            $seo = $cmsPage->seoComponent()->first();
        }

        return view('pages.doctor-filter', compact('filterDoctors', 'hospitals', 'seo'));
    }
}