【发布时间】:2014-10-06 09:10:18
【问题描述】:
在我的 Laravel 应用程序中,它基本上是一个用于许多“资源”(客户、帐户等)的 CRUD 应用程序,我想创建一个审计日志。这将使我能够查看应用程序中任何资源的任何创建、删除或编辑、发生时间以及更改的用户。
我首先创建了以下架构(以及关联的模型Change):
Schema::create('changes', function(Blueprint $table)
{
$table->increments('id');
$table->dateTime('change_date')->nullable(); //The time the change was made
$table->string('model'); // The model the change was made to
$table->text('change'); // What was changed (e.g. `name` field for client 10015 was changed from "John" to "Johnny")
$table->integer('user_id')->unsigned()->nullable(); // The user who made the change
$table->timestamps();
});
现在,每当对另一个模型进行更改时,我希望我的应用程序在更改表中插入一条记录。例如。假设一个客户端被编辑,一些额外的代码需要在Client->save() 之后运行。比如:
$change = new Change;
$change->user_id = User()->id;
$change->model = // ? how do I get the name of the model just changed?
$change->change = 'User 10015 edited: name: "John" -> "Johnny"; active: 1 -> 0;'; // somehow get the fields that were changed. Different for delete and create.
$change->save();
我想我需要制作 3 个过滤器(或事件侦听器?),每个过滤器用于创建、编辑和删除。但是我如何传递所需的数据(如字段和模型名称)?是否有一个已经这样做的包?任何帮助表示赞赏。
编辑 revisionable 包似乎完成了我想要的 95% - 只是不记录创建和删除。
【问题讨论】: