Update Log aktivitas
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
||||
use Spatie\Activitylog\Support\LogOptions;
|
||||
|
||||
trait CatatAktivitas
|
||||
{
|
||||
use LogsActivity;
|
||||
|
||||
protected static array $kolomTidakDicatat = [
|
||||
'created_by', 'updated_by', 'deleted_by', 'created_at', 'updated_at', 'deleted_at',
|
||||
'password', 'remember_token', 'sync_status',
|
||||
'foto', 'foto_url', 'path', 'path_kk', 'path_ktp', 'file_kk', 'file_ktp',
|
||||
];
|
||||
|
||||
protected static array $kataEvent = [
|
||||
'created' => 'ditambahkan',
|
||||
'updated' => 'diubah',
|
||||
'deleted' => 'dihapus',
|
||||
'restored' => 'dipulihkan',
|
||||
];
|
||||
|
||||
// Aturan log: kolom fillable yang berubah saja
|
||||
public function getActivitylogOptions(): LogOptions
|
||||
{
|
||||
$label = property_exists($this, 'labelAktivitas') ? $this->labelAktivitas : Str::headline(class_basename($this));
|
||||
$logName = property_exists($this, 'logAktivitas') ? $this->logAktivitas : Str::snake(explode('\\', static::class)[2]);
|
||||
|
||||
return LogOptions::defaults()
|
||||
->useLogName($logName)
|
||||
->logFillable()
|
||||
->logExcept([...self::$kolomTidakDicatat, ...$this->getHidden()])
|
||||
->dontLogIfAttributesChangedOnly(self::$kolomTidakDicatat)
|
||||
->logOnlyDirty()
|
||||
->dontLogEmptyChanges()
|
||||
->setDescriptionForEvent(fn (string $event) => $label . ' ' . (self::$kataEvent[$event] ?? $event));
|
||||
}
|
||||
|
||||
// Tandai petani terkait
|
||||
public function beforeActivityLogged(Model $activity, string $event): void
|
||||
{
|
||||
if ($petaniId = $this->getAttribute('petani_id')) {
|
||||
$activity->properties = $activity->properties->put('petani_id', (int) $petaniId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user