簡體   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