繁体   English   中英

如何在简单的CRUD laravel中执行简单的审核跟踪

[英]How to perform simple audit trail in simple CRUD laravel

如何使用laravel中的( updated_at )时间戳获取最后一次更新表单的用户的用户名,以及最佳的逻辑或方法是什么?是否可以仅在Blade中使用? 还是我需要将代码放入控制器?

示例:已 更新:2018年6月13日更新者 :用户名

目前,我只能在刀片中使用此代码来获取更新日期

Updated:{{ date('d-M-Y', strtotime($leads->updated_at))}}

您的数据库表中需要这两列: updated_atlast_updated_by

在控制器中,当您更新记录时,请在last_updated_by字段中添加正在更新记录的用户的ID。

注意 :这将仅显示由用户ID更新的最新信息。

如果要存储每个updated_by,则创建一个新表:

   table:  update_timeline

   postid : user_id : updated_date 

现在,您可以对这两个表使用联接查询,并根据需要更新每个表。

数据库表

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;

楷模

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');
    }
}

现在获取线索

$leads = Lead::all(); //for demo purpose, always use pagination based results or fetch with condition

这是模板渲染

@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

假设您的用户表带有“用户名”字段

每当用户更新潜在客户时,您只需要在潜在客户历史记录表中插入一条记录,并在update_by字段中使用该userId

您可以将模型更改存储在单独的模型中,例如Activity 该模型可以与模型具有多友关系,对执行活动的用户具有外键,并且可以具有操作类型(即createupdatedelete )。

然后,您可以创建适用于模型的特征,该特征在每次创建,更新或删除模型时自动创建活动记录:

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();
    }
}

然后,您可以粗略列出模型的操作,即

  1. 用户1创建了模型1
  2. 用户2更新了模型1
  3. 用户1创建了模型2
  4. 用户1删除了模型1

…等等。

我已经为created_by,updated_by审计跟踪创建了一个程序包。 您可以在https://github.com/insenseanalytics/laravel-user-audit-trails上查看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM