Update Spatie versi 4
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace App\Models\Concerns;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
||||
use Spatie\Activitylog\Support\LogOptions;
|
||||
use Spatie\Activitylog\Contracts\Activity;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
|
||||
trait CatatAktivitas
|
||||
{
|
||||
@@ -36,15 +36,19 @@ trait CatatAktivitas
|
||||
->logExcept([...self::$kolomTidakDicatat, ...$this->getHidden()])
|
||||
->dontLogIfAttributesChangedOnly(self::$kolomTidakDicatat)
|
||||
->logOnlyDirty()
|
||||
->dontLogEmptyChanges()
|
||||
->dontSubmitEmptyLogs()
|
||||
->setDescriptionForEvent(fn (string $event) => $label . ' ' . (self::$kataEvent[$event] ?? $event));
|
||||
}
|
||||
|
||||
// Tandai petani terkait
|
||||
public function beforeActivityLogged(Model $activity, string $event): void
|
||||
// Simpan nama data & petani terkait di log (halaman log tak perlu memuat data asli)
|
||||
public function tapActivity(Activity $activity, string $eventName): void
|
||||
{
|
||||
if ($petaniId = $this->getAttribute('petani_id')) {
|
||||
$activity->properties = $activity->properties->put('petani_id', (int) $petaniId);
|
||||
}
|
||||
$label = $this->getAttribute('nama') ?? $this->getAttribute('name');
|
||||
$petaniId = $this->getAttribute('petani_id');
|
||||
|
||||
$activity->properties = $activity->properties->merge(array_filter([
|
||||
'label' => is_string($label) ? Str::limit($label, 100) : null,
|
||||
'petani_id' => $petaniId ? (int) $petaniId : null,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Konfigurasi;
|
||||
|
||||
use App\Services\Konfigurasi\BufferLogAktivitas;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
class LogAktivitas extends Activity
|
||||
{
|
||||
// Saat request web/API, log ditampung lalu disimpan sekaligus di akhir request
|
||||
public function save(array $options = []): bool
|
||||
{
|
||||
$buffer = app(BufferLogAktivitas::class);
|
||||
|
||||
if ($this->exists || ! $buffer->aktif()) {
|
||||
return parent::save($options);
|
||||
}
|
||||
|
||||
$this->updateTimestamps();
|
||||
$buffer->tambah($this->getAttributes());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user