simedkom/app/Models/News.php
2025-07-16 15:14:04 +07:00

61 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
class News extends Model
{
use SoftDeletes, LogsActivity;
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}";
}
}
}