[英]How to perform simple audit trail in simple CRUD laravel
how to get the username of the last person who update the form,using the ( updated_at ) timestamp in laravel and what is the best logic or approach to do?is it possible to use in blade only? 如何使用laravel中的( updated_at )时间戳获取最后一次更新表单的用户的用户名,以及最佳的逻辑或方法是什么?是否可以仅在Blade中使用? or should i need to put the code in my controller?
还是我需要将代码放入控制器?
Example: Updated:13-Jun-2018 UpdatedBy: Username 示例:已 更新:2018年6月13日更新者 :用户名
currently i can only get the updated date by using this code in my blade 目前,我只能在刀片中使用此代码来获取更新日期
Updated:{{ date('d-M-Y', strtotime($leads->updated_at))}}
You need this two columns in your database table : updated_at
and last_updated_by
您的数据库表中需要这两列:
updated_at
和last_updated_by
in your controller , when you are updating that record , add id of user who is updating the record in last_updated_by
field . 在控制器中,当您更新记录时,请在
last_updated_by
字段中添加正在更新记录的用户的ID。
note : this will display only last updated by user id . 注意 :这将仅显示由用户ID更新的最新信息。
if you want to store each updated_by then make a new table : 如果要存储每个updated_by,则创建一个新表:
table: update_timeline
postid : user_id : updated_date
now you can use join query for these two tables and get every updated by , if you want . 现在,您可以对这两个表使用联接查询,并根据需要更新每个表。
Database Table 数据库表
CREATE TABLE `leads` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `lead_history` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lead_id` int(11) unsigned DEFAULT NULL,
`updated_by` int(11) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Models
楷模
class Lead extends Model {
public function history()
{
return $this->hasMany(LeadHistory::class,'lead_id');
}
}
class LeadHistory extends Model{
public function lead()
{
return $this->belongsTo(Lead::class);
}
public function user()
{
return $this->belongsTo(User::class, 'updated_by');
}
}
Now Fetch the leads
现在获取线索
$leads = Lead::all(); //for demo purpose, always use pagination based results or fetch with condition
Here is the template rendering
这是模板渲染
@foreach ($leads as $lead)
<p>This is your lead: {{ $lead->name }}</p>
@if ($lead->history)
<label>Lead Updated By:</label>
<ul>
@foreach ($lead->history as $history)
<li>{{$history->user->username}}</li>
@endforeach
</ul>
@endif
@endforeach
Assuming you have user table with "username" field 假设您的用户表带有“用户名”字段
Whenever user update the lead, you just need to insert a record in lead history table with that userId in updated_by field 每当用户更新潜在客户时,您只需要在潜在客户历史记录表中插入一条记录,并在update_by字段中使用该userId
You could store model changes in a separate model, say Activity
. 您可以将模型更改存储在单独的模型中,例如
Activity
。 This model could have a polymoprhic relationship to a model, a foreign key to the user that performed the activity, and the type of operation (ie create
, update
, delete
). 该模型可以与模型具有多友关系,对执行活动的用户具有外键,并且可以具有操作类型(即
create
, update
, delete
)。
You could then create a trait that you apply to models that automatically creates an activity record each time a model is created, updated, or deleted: 然后,您可以创建适用于模型的特征,该特征在每次创建,更新或删除模型时自动创建活动记录:
trait Auditable
{
public static function bootAuditable()
{
static::created(function ($model) {
$model->recordActivity('create');
});
static::updated(function ($model) {
$model->recordActivity('update');
});
static::deleted(function ($model) {
$model->recordActivity('delete');
});
}
protected function recordActivity($operation)
{
$activity = new Activity([
'operation' => $operation,
'user_id' => Auth::id(),
]);
$activity->model()->associate($this);
$activity->save();
}
}
You'll then be able to crudely list operations for a model, ie 然后,您可以粗略列出模型的操作,即
…and so on. …等等。
I've created a package for created_by, updated_by audit trails. 我已经为created_by,updated_by审计跟踪创建了一个程序包。 You can check it out at https://github.com/insenseanalytics/laravel-user-audit-trails
您可以在https://github.com/insenseanalytics/laravel-user-audit-trails上查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.