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'));
}
}