File: /var/www/html/sarvodayahospital/app/Models/Doctor.php
<?php
// phpcs:disable
namespace App\Models;
use App\Scopes\isActiveScope;
use App\Scopes\isPublishedScope;
use Illuminate\Database\Eloquent\Model;
class Doctor extends Model
{
protected $with = ['specialities', 'treatments', 'media', 'gender', 'blogs', 'hospitals'];
protected $hidden = ['published_at', 'created_by', 'updated_by', 'created_at', 'updated_at'];
protected $appends = ['AltInfo'];
public function getDoctorNameAttribute($value)
{
return ucwords($value);
}
public function getAltInfoAttribute() {
$specialities = collect($this->specialities)->toArray();
$mapSpecialities = array_map(function ($value) {
return $value['SpecialityName'];
}, $specialities);
$allSpecialities = implode(',', $mapSpecialities);
return $this->DoctorName. (count($specialities) ? ' | ' .$allSpecialities. ' | ' : ' | ') . 'Sarvodaya Hospital';
}
public function implode_all($glue, $arr){
for ($i=0; $i<count($arr); $i++) {
if (@is_array($arr[$i]))
$arr[$i] = $this->implode_all($glue, $arr[$i]);
}
return $arr;
}
public function getDescriptionAttribute($value)
{
return html_entity_decode($value);
}
public function getAboutSpecialityAttribute($value)
{
return html_entity_decode($value);
}
public function getEducationAttribute($value)
{
return html_entity_decode($value);
}
public function getWorkExperienceAttribute($value)
{
return html_entity_decode($value);
}
public function getAwardRecognitionAttribute($value)
{
return html_entity_decode($value);
}
public function getMembershipAttribute($value)
{
return html_entity_decode($value);
}
public function getContributionToSocietyAttribute($value)
{
return html_entity_decode($value);
}
public function getOtherInformationAttribute($value)
{
return html_entity_decode($value);
}
protected static function booted()
{
static::addGlobalScope(new isActiveScope());
static::addGlobalScope(new isPublishedScope());
}
public function hospitals()
{
return $this->belongsToMany(
Hospital::class,
'doctors__hospitals',
'doctor_id',
'hospital_id'
);
}
public function blogs()
{
return $this->hasMany(
Blog::class,
'Doctor',
'id'
);
}
public function specialities()
{
return $this->belongsToMany(
Speciality::class,
'doctors__specialities',
'doctor_id',
'speciality_id'
)->select('SpecialityName', 'Slug');
}
public function treatments()
{
return $this->belongsToMany(
Treatment::class,
'doctors__treatments',
'doctor_id',
'treatment_id'
)->select('TreatmentName', 'Slug');
}
public function media()
{
return $this->hasOneThrough(
Media::class,
MediaMorph::class,
'related_id',
'id',
'id',
'upload_file_id'
)
->where('related_type', 'doctors')
->where('field', 'Photo')
->orderBy('order')
->select('upload_file.id', 'name', 'url', 'formats');
}
public function gender()
{
return $this->belongsTo(Gender::class, 'Gender')
->select(['id', 'Gender', 'Slug']);
}
public function patientTestimonials()
{
return $this->belongsToMany(
Testimonial::class,
'testimonials__doctors',
'doctor_id',
'testimonial_id'
)->select([
'testimonials.id',
'Title',
'Slug',
'YoutubeVideoCode',
'Date',
'PatientName',
'PatientLocation',
'Date',
]);
}
public function profilePhoto()
{
return $this->hasOne(MediaMorph::class, 'related_id')
->where('field', 'Photo')
->where('related_type', 'doctors');
}
public function seoComponent()
{
return $this->hasOneThrough(
SEOComponent::class,
DoctorComponent::class,
'doctor_id',
'id',
'id',
'component_id'
)->where('field', 'SEO')
->where('component_type', 'components_seo_seos');
}
public function mednetInformation()
{
return $this->hasManyThrough(
MednetComponent::class,
DoctorComponent::class,
'doctor_id',
'id',
'id',
'component_id'
)->where('field', 'DoctorMednetInfo')
->where('component_type', 'components_mednet_mednets');
}
public function enableForOnlineBooking()
{
$doctorAvailability = $this->mednetInformation();
if(!is_null($doctorAvailability)) {
return $doctorAvailability->pluck('BookingEnabled')->contains(1);
}
}
public static function getDoctorBySpecialityAndHospital($specialities, $hospitals) {
return static::with('specialities', 'hospitals')
->whereHas('specialities', function($query) use($specialities) {
$query->whereIn('specialities.id', $specialities);
})
->whereHas('hospitals', function($query) use($hospitals) {
$query->whereIn('hospitals.id', $hospitals);
})
->orderByRaw('ISNULL(Weight), Weight ASC')
->get();
}
public static function getDoctorByHospital($hospitals) {
return static::with('hospitals')
->whereHas('hospitals', function($query) use($hospitals) {
$query->whereIn('hospitals.id', $hospitals);
})
->orderByRaw('ISNULL(Weight), Weight ASC')
->paginate(40);
}
}