【问题标题】:Spatie Laravel-activitylog causedBy, performedOn with Logging modelSpatie Laravel-activitylog 由 Logging 模型执行
【发布时间】:2018-07-04 16:44:38
【问题描述】:

我正在使用this package for activity logging in laravel 我可以从控制器进行日志记录,但我想使用模型来进行。

我从official documentation阅读了这些有用的信息

但是,它不存储主题 ID、类型和原因 ID、类型。我可以将它从控制器存储为

activity()
   ->causedBy($userModel)
   ->performedOn($someContentModel)
   ->log('edited');

如何从模型中做到这一点?欢迎提出建议。

【问题讨论】:

    标签: php laravel-5 activitylog


    【解决方案1】:

    好的。现在我得到了你的问题。如果你想在模态中表现。 下面是我在Business 模型类中的示例代码。

     protected static function boot()
        {
            //to log what field update
            static::updating(function ($business) {
                $changes = $business->isDirty() ? $business->getDirty() : false;
                if($changes)
                {
                    foreach($changes as $attr => $value)
                    {
                        activity()
                            ->performedOn($business)
                            ->causedBy(auth()->user())
                            ->withProperties(['business_name' => $business->name, 'which field updated' => $business->getDirty()])
                            ->log('Business Field <span class="text-green">Updated</span>  - '.$business->name);
    
                    }
                }
            });
        }
    

    对于您必须手动添加的主题信息,以下是我如何将其存储在控制器中的示例代码。希望大家可以参考一下。

    activity() 
        ->performedOn($business)
        ->causedBy(auth()->user())
        ->withProperties(['business_name' => $business->name)
        ->log('Business <span class="text-green">Updated</span>  - '.$business->name);
    

    数据库记录如下:

    +----+----------+-----------------------------------------------------------------+------------+--------------+-----------+-------------+-------------------------------------+---------------------+---------------------+
    | id | log_name | description                                                     | subject_id | subject_type | causer_id | causer_type | properties                          | created_at          | updated_at          |
    +----+----------+-----------------------------------------------------------------+------------+--------------+-----------+-------------+-------------------------------------+---------------------+---------------------+
    |  1 | default  | Business <span class="text-green">Updated</span> - Companies 10 |         10 | App\Business |         1 | App\User    | {"business_name":"Best Restaurant"} | 2017-08-04 14:58:06 | 2017-08-04 14:58:06 |
    +----+----------+-----------------------------------------------------------------+------------+--------------+-----------+-------------+-------------------------------------+---------------------+---------------------+
    

    【讨论】:

    • 感谢您的光临,是的,我必须手动添加,但我没有在模型中得到正确的使用示例。你有什么想法吗? :) 我不想在控制器方法中添加我的日志记录代码。因此,使用模型将变得快速而简单
    • 你的意思是你没有得到正确的使用?我在上面的答案中更新了我的数据库记录。只是为了确保您使用与我相同的版本。我正在使用 v2。但是您分享的链接是 v1。
    • 谢谢,我从中得到了一些想法
    • 如果可能,请分享您的解决方案并标记为已完成。
    猜你喜欢
    • 2021-03-09
    • 2018-07-09
    • 2019-03-30
    • 1970-01-01
    • 2023-01-21
    • 2019-08-19
    • 2018-05-27
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多