star-cloud/app/Models/System/SystemOperationLog.php
sky121113 87c4c281a1 [FEAT] 新增廣告狀態切換功能與優化商品操作紀錄介面
1. 新增廣告管理狀態切換 (Toggle Status) 功能,包含後端 API、路由與前端 UI 實作。
2. 優化商品操作紀錄 (Audit Logs) 的過濾介面,統一使用 x-searchable-select 並修正搜尋圖示樣式。
3. 擴充 SystemOperationLog 模型,支援自動解析操作目標名稱 (含 code 欄位備援)。
4. 優化商品操作紀錄查詢效能,預載入 (Eager Load) target 關聯資料。
5. 更新多語系檔案 (zh_TW, en, ja),新增廣告狀態切換相關文字。
2026-05-14 13:02:08 +08:00

98 lines
2.2 KiB
PHP

<?php
namespace App\Models\System;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\TenantScoped;
use App\Models\System\User;
use App\Models\System\Company;
class SystemOperationLog extends Model
{
use HasFactory, TenantScoped;
protected $appends = [
'translated_note',
];
/**
* 動態翻譯備註
*/
public function getTranslatedNoteAttribute(): ?string
{
if (empty($this->note)) {
return null;
}
// 過濾掉非字串/數值的數值,避免 Laravel 翻譯引擎處理巢狀陣列時報錯
$replacements = collect(array_merge(
$this->old_values ?? [],
$this->new_values ?? []
))->filter(fn($val) => is_scalar($val))->toArray();
return __($this->note, $replacements);
}
protected $fillable = [
'company_id',
'user_id',
'module',
'action',
'target_id',
'target_type',
'old_values',
'new_values',
'note',
'ip_address',
'user_agent',
];
protected $casts = [
'old_values' => 'array',
'new_values' => 'array',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function company()
{
return $this->belongsTo(Company::class);
}
/**
* Polymorphic relationship to the target entity
*/
public function target()
{
return $this->morphTo()->withTrashed();
}
/**
* Get the name of the target entity from relation or values
*/
public function getTargetNameAttribute(): string
{
// 1. Try from loaded relationship
if ($this->target) {
if (method_exists($this->target, 'getLocalizedNameAttribute')) {
return $this->target->localized_name;
}
return $this->target->name ?? '';
}
// 2. Try from values
$name = $this->new_values['name'] ?? $this->old_values['name'] ??
$this->new_values['code'] ?? $this->old_values['code'] ?? null;
if ($name) {
return $name;
}
return '';
}
}