*/ protected $fillable = [ 'practitioner_id', 'doc_type', 'file_id', 'issue_date', 'expiry_date', 'status', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'issue_date' => 'date', 'expiry_date' => 'date', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Get the thanatopractitioner that owns the document. */ public function thanatopractitioner(): BelongsTo { return $this->belongsTo(Thanatopractitioner::class, 'practitioner_id'); } /** * Scope a query to only include documents with valid expiry date. */ public function scopeValid($query) { return $query->where(function ($q) { $q->whereNull('expiry_date') ->orWhere('expiry_date', '>=', now()); }); } /** * Scope a query to only include documents with expired expiry date. */ public function scopeExpired($query) { return $query->whereNotNull('expiry_date') ->where('expiry_date', '<', now()); } /** * Scope a query to filter by document type. */ public function scopeOfType($query, string $type) { return $query->where('doc_type', $type); } /** * Check if the document is still valid. */ public function getIsValidAttribute(): bool { if (!$this->expiry_date) { return true; // No expiry date means it's valid } return $this->expiry_date >= now(); } }