我正在使用this package for activity logging in laravel,我可以从控制器做日志,但是我想用模型来做。
我从official documentation上阅读了这些有用的信息
但是,它不存储主题id,type和causer id,type。我可以从控制器中将其存储为
activity()
->causedBy($userModel)
->performedOn($someContentModel)
->log('edited');如何在模型中做到这一点?我们非常感谢您的建议。
发布于 2018-01-25 20:30:33
好的。现在我明白你的问题了。如果你想在Modal中表演。下面是我的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 |
+----+----------+-----------------------------------------------------------------+------------+--------------+-----------+-------------+-------------------------------------+---------------------+---------------------+https://stackoverflow.com/questions/48442798
复制相似问题