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

82 lines
2.0 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 Order extends Model
{
use LogsActivity, SoftDeletes;
protected $table = 'orders';
protected $fillable = [
'start_date',
'end_date',
'image',
'task_description',
'required_reports',
'cooperation_id',
'enhancer_id',
];
public function media()
{
return $this->belongsToMany(Media::class, 'media_orders', 'order_id', 'media_id')
->using(MediaOrder::class)
->withPivot(['status', 'rejection_reason'])
->withTimestamps();
}
public function cooperation()
{
return $this->belongsTo(Cooperation::class, 'cooperation_id', 'id')->withTrashed();
}
public function reports()
{
return $this->hasMany(Report::class, 'order_id', 'id');
}
public function completions()
{
return $this->hasMany(CooperationCompletion::class, 'order_id', 'id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly([
'start_date',
'end_date',
'image',
'task_description',
'required_reports',
'cooperation_id',
'enhancer_id',
]) // kolom yang dicatat
->useLogName('order') // 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;
}
return "Media Order telah {$eventName}";
}
}