Laravel Spatie - 通过模式审核日志时的自定义属性值
在我的模态上,我有两个函数,用于在数据更改时记录数据。这些在下面。
namespace App\Models;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Contracts\Activity;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Receivinglogentry extends Model
{
use HasFactory;
use LogsActivity;
protected $fillable = [
'status',
'amt_shipment',
'container',
'po',
'etd_date',
'eta_date',
];
protected $casts = [
'po_ref' => 'json',
];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['*'])->logOnlyDirty();
}
public function tapActivity(Activity $activity,string $eventName)
{
$current_user = Auth::user()->name;
$event = $activity->attributes['event'];
$data = $activity->relations['subject']->attributes['container'];
$masterID = $activity->relations['subject']->attributes['id'];
$activity->description = "{$current_user} has {$event} Container : {$data}";
$activity->causer_name = $current_user;
$activity->master_id = $masterID ;
$activity->log_name = 'Receivinglogentry';
}
}
fillable
数据status
已存储为整数值。但我必须将其记录为字符串值,例如 PENDING
或 ACTIVE
。任何有关自定义记录属性的建议均适用。
on my modal, I have two functions which I have used to log the data when it has been changed. those are below.
namespace App\Models;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Contracts\Activity;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Receivinglogentry extends Model
{
use HasFactory;
use LogsActivity;
protected $fillable = [
'status',
'amt_shipment',
'container',
'po',
'etd_date',
'eta_date',
];
protected $casts = [
'po_ref' => 'json',
];
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['*'])->logOnlyDirty();
}
public function tapActivity(Activity $activity,string $eventName)
{
$current_user = Auth::user()->name;
$event = $activity->attributes['event'];
$data = $activity->relations['subject']->attributes['container'];
$masterID = $activity->relations['subject']->attributes['id'];
$activity->description = "{$current_user} has {$event} Container : {$data}";
$activity->causer_name = $current_user;
$activity->master_id = $masterID ;
$activity->log_name = 'Receivinglogentry';
}
}
fillable
data status
has been stored as an integer value. but I have to log it as a string value something like PENDING
or ACTIVE
. any recommendation to log attributes customizably is appricated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试 ENUM 概念,但从 Laravel 9 开始。这是一个参考链接 - https://enversanli。 medium.com/how-to-use-enums-with-laravel-9-d18f1ee35b56
他们在那里描述了一个枚举
UserRoleEnum:string
您可以根据自己的要求进行格式化。就您而言,密钥本身就是一个数字。所以我建议将其设置为如下字符串。
然后使用您的可填充状态来调用它,例如
"STATUS_" + $fillable.status
如果不使用 Laravel 9,您可以尝试如下所述:
https://stackoverflow.com/a/55298089/2086966
然后将规则编写为:
You can try ENUM concept but in Laravel 9 onwards. Here is a reference link - https://enversanli.medium.com/how-to-use-enums-with-laravel-9-d18f1ee35b56
There they describe about an enum
UserRoleEnum:string
which you can format as your own requirement.In your case, the key itself is a number. So I suggest to make it as string as below.
And then call it with your fillable status something like
"STATUS_" + $fillable.status
If not using Laravel 9, you may try as below described in :
https://stackoverflow.com/a/55298089/2086966
and then write the rule as: