File: /var/www/html/sarvodayahospital/app/Models/HindiBlog.php
<?php
namespace App\Models;
use App\Scopes\isActiveScope;
use App\Scopes\isPublishedScope;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class HindiBlog extends Model
{
protected $table = 'blog_post_hindis';
protected $with = ['media', 'doctorInfo', 'specialities'];
protected static function booted()
{
static::addGlobalScope(new isActiveScope());
static::addGlobalScope(new isPublishedScope());
}
public function media()
{
return $this->hasOneThrough(
Media::class,
MediaMorph::class,
'related_id',
'id',
'id',
'upload_file_id'
)
->where('related_type', 'blog_post_hindis')
->where('field', 'Image')
->orderBy('order')
->select('upload_file_morph.id', 'name', 'url', 'formats');
}
public function doctorInfo()
{
return $this->belongsTo(Doctor::class, 'Doctor', 'id')
->without('specialities', 'gender', 'blogs', 'hospitals')
->select('id', 'DoctorName', 'Slug', 'CurrentDesignation');
}
public function specialities()
{
return $this->belongsToMany(
Speciality::class,
'blog_post_hindis__specialities',
'blog_post_hindi_id',
'speciality_id'
)->select('SpecialityName', 'Slug');
}
public function seoComponent()
{
return $this->hasOneThrough(
SEOComponent::class,
HindiBlogComponent::class,
'blog_post_hindi_id',
'id',
'id',
'component_id'
)->where('field', 'SEO')
->where('component_type', 'components_seo_seos');
}
public function getPublishedAtAttribute($value)
{
return Carbon::parse($value)->format('M d, Y');
}
public static function findBySlug($slug) {
return static::query()
->where('Slug', $slug)
->firstOrFail();
}
public static function getDoctorBlog($doctor_id, $slug) {
return static::query()
->with('doctorInfo')
->whereHas('doctorInfo', function($query) use($doctor_id) {
$query->where('id', $doctor_id);
})
->where('Slug', '!=', $slug)
->take(3)
->get();
}
public static function getRecentBlog($limit = 10) {
return static::query()
->orderBy('published_at', 'ASC')
->take($limit)
->get();
}
}