Laravel Spatie - 通过模式审核日志时的自定义属性值

发布于 2025-01-13 05:18:51 字数 1541 浏览 0 评论 0原文

在我的模态上,我有两个函数,用于在数据更改时记录数据。这些在下面。

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 已存储为整数值。但我必须将其记录为字符串值,例如 PENDINGACTIVE。任何有关自定义记录属性的建议均适用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2025-01-20 05:18:51

你可以尝试 ENUM 概念,但从 Laravel 9 开始。这是一个参考链接 - https://enversanli。 medium.com/how-to-use-enums-with-laravel-9-d18f1ee35b56

他们在那里描述了一个枚举UserRoleEnum:string 您可以根据自己的要求进行格式化。
就您而言,密钥本身就是一个数字。所以我建议将其设置为如下字符串。

enum StatusEnum:string
{
    case STATUS_101 = 'Pending';
    case STATUS_34 = 'Completed';
    case STATUS_22 = 'On hold';
}

然后使用您的可填充状态来调用它,例如 "STATUS_" + $fillable.status


如果不使用 Laravel 9,您可以尝试如下所述:
https://stackoverflow.com/a/55298089/2086966

class MyClass {
  const DEFAULT = 'default';
  const SOCIAL = 'social';
  const WHATEVER = 'whatever';
  public static $types = [self::DEFAULT, self::SOCIAL, self::WHATEVER];

然后将规则编写为:

'type' => Rule::in(MyClass::$types)

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.

enum StatusEnum:string
{
    case STATUS_101 = 'Pending';
    case STATUS_34 = 'Completed';
    case STATUS_22 = 'On hold';
}

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

class MyClass {
  const DEFAULT = 'default';
  const SOCIAL = 'social';
  const WHATEVER = 'whatever';
  public static $types = [self::DEFAULT, self::SOCIAL, self::WHATEVER];

and then write the rule as:

'type' => Rule::in(MyClass::$types)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文