63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\SI;
|
|
|
|
use App\Models\ArchiveHistory;
|
|
use App\Models\ArchiveInfo;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class SttMedicalHistory extends Model
|
|
{
|
|
protected $table = 'si_stt_patients';
|
|
|
|
protected $fillable = [
|
|
'family', // Фамилия
|
|
'name', // Имя
|
|
'ot', // Отчество
|
|
'daterecipient', // Д. пост. (mpostdate)
|
|
'dateextract', // Д. вып. (menddate)
|
|
'narhiv', // № в архиве
|
|
'datearhiv', // Д. архив
|
|
'statgod', // Год нахождения в стационаре
|
|
'enp', // ЕНП
|
|
'medcardnum', // № карты
|
|
'dr', // ДР
|
|
];
|
|
|
|
public function getFullNameAttribute()
|
|
{
|
|
return "$this->family $this->name $this->ot";
|
|
}
|
|
|
|
public function archiveHistory()
|
|
{
|
|
return $this->morphMany(ArchiveHistory::class, 'historyable');
|
|
}
|
|
|
|
public function archiveInfo()
|
|
{
|
|
return $this->morphOne(ArchiveInfo::class, 'historyable');
|
|
}
|
|
|
|
public function scopeSearch($query, $searchText)
|
|
{
|
|
return $query->where(function($q) use ($searchText) {
|
|
if (is_numeric($searchText)) {
|
|
$q->where('medcardnum', 'ILIKE', "$searchText");
|
|
} else {
|
|
// Ищем в объединенном ФИО и в отдельных полях
|
|
$searchPattern = "%{$searchText}%";
|
|
|
|
$q->where(function($subQ) use ($searchPattern) {
|
|
// Поиск в объединенной строке
|
|
$subQ->whereRaw("CONCAT(family, ' ', name, ' ', COALESCE(ot, '')) ILIKE ?", [$searchPattern])
|
|
// И дополнительно в отдельных полях для точности
|
|
->orWhere('family', 'ILIKE', $searchPattern)
|
|
->orWhere('name', 'ILIKE', $searchPattern)
|
|
->orWhere('ot', 'ILIKE', $searchPattern);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|