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

71 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class Photo extends Model
{
use HasFactory, SoftDeletes, LogsActivity;
protected $fillable = [
'person_id',
'filename',
'original_filename',
'file_path',
'mime_type',
'file_size',
'is_profile_photo',
'caption',
];
protected $casts = [
'is_profile_photo' => 'boolean',
];
// 🔧 Configure Spatie logging
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->useLogName('photo')
->logFillable()
->logOnlyDirty();
}
// 📝 Custom activity description for events
public function getDescriptionForEvent(string $eventName): string
{
return match ($eventName) {
'created' => 'Added new photo',
'updated' => 'Updated photo details',
'deleted' => 'Deleted photo',
default => ucfirst($eventName) . ' photo record',
};
}
// 🔗 Relationships
public function person()
{
return $this->belongsTo(Person::class, 'person_id', 'person_id');
}
// Helper method to set a photo as profile photo
public function setAsProfilePhoto(): bool
{
// First unset any existing profile photo for this person
if ($this->person_id) {
self::where('person_id', $this->person_id)
->where('is_profile_photo', true)
->update(['is_profile_photo' => false]);
}
// Set this photo as profile photo
$this->is_profile_photo = true;
return $this->save();
}
}