simedkom/app/Models/News.php
2025-10-13 08:35:36 +07:00

63 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
class News extends Model
{
use LogsActivity, SoftDeletes;
protected $table = 'news';
protected $fillable = [
'title',
'slug',
'content',
'image',
'link',
'tag',
'category',
'views',
'author_id',
];
public function author()
{
return $this->belongsTo(User::class, 'author_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['title', 'slug', 'content', 'image', 'link', 'category', 'author_id']) // kolom yang dicatat
->useLogName('news') // nama log
->logOnlyDirty(); // hanya log kalau ada perubahan
}
public function getDescriptionForEvent(string $eventName): string
{
switch ($eventName) {
case 'created':
$eventName = 'dibuat';
break;
case 'updated':
$eventName = 'diubah';
break;
case 'deleted':
$eventName = 'dihapus';
break;
}
if ($this->category == '0') {
return "Berita '{$this->title}' telah {$eventName}";
} else {
return "Pengumuman '{$this->title}' telah {$eventName}";
}
}
}