migrants-nt-sec/app/Models/Person.php

95 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Person extends Model
{
use HasFactory, SoftDeletes, Searchable, LogsActivity;
protected $table = 'person';
protected $primaryKey = 'person_id';
protected $fillable = [
'surname',
'christian_name',
'full_name',
'date_of_birth',
'place_of_birth',
'date_of_death',
'occupation',
'additional_notes',
'reference',
'id_card_no',
];
protected $casts = [
'date_of_birth' => 'date',
'date_of_death' => 'date',
];
// 🔧 Configure Spatie logging
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->useLogName('person')
->logFillable()
->logOnlyDirty();
}
// 📝 Custom activity description for events
public function getDescriptionForEvent(string $eventName): string
{
return match ($eventName) {
'created' => 'Added new migrant',
'updated' => 'Updated migrant details',
'deleted' => 'Deleted migrant record',
default => ucfirst($eventName) . ' migrant record',
};
}
// 🔗 Relationships
public function migration()
{
return $this->hasOne(Migration::class, 'person_id');
}
public function naturalization()
{
return $this->hasOne(Naturalization::class, 'person_id');
}
public function residence()
{
return $this->hasOne(Residence::class, 'person_id');
}
public function family()
{
return $this->hasOne(Family::class, 'person_id');
}
public function internment()
{
return $this->hasOne(Internment::class, 'person_id');
}
public function toSearchableArray()
{
return [
'person_id' => $this->person_id,
'surname' => $this->surname,
'christian_name' => $this->christian_name,
'full_name' => $this->full_name,
'place_of_birth' => $this->place_of_birth,
'id_card_no' => $this->id_card_no,
];
}
}