imanghafoori/eloquent-history

此包最新版本(v0.0.4)没有提供许可证信息。

v0.0.4 2023-02-15 16:23 UTC

This package is auto-updated.

Last update: 2024-09-16 13:05:54 UTC


README

它跟踪您的表行,就像git一样,它只为每次更新记录变更。

Latest Stable Version

安装

composer require imanghafoori/eloquent-history

php artisan vendor:publish

php artisan migrate

使用

public function boot()
{
    // here we want to monitor all the table columns except 'remember_token'
    HistoryTracker::track('App/User', $except = ['remember_token']);
}

注意:作为在服务提供程序内注册 Hisotry Tracker 的替代方法,您可以在所需的模型(s)内简单地使用 WithHistoryTracker 特性。

use Imanghafoori\EloquentHistory\WithHistoryTracker;

class User extends Authenticatable
{
    use WithHistoryTracker;
    
    // here we want to monitor all the table columns except 'remember_token'
    private static $historyTrackerExceptions = ['remember_token']; 
    ...
}

** 注意 ** 由于这是基于eloquent模型事件工作的,如果您在未触发事件的情况下更新行,则更改将不会记录。

这包括在没有先获取行的情况下执行更新查询。

所以举个例子

// This query can NOT be monitored.
User::update([...]);

公共API

// Get all the history as a nice table

HistoryTracker::getHistoryOf(Model $model, array $columns, array $importantCols = []);

// It performs a query on the data changes table and gives you a raw version of changes.

HistoryTracker::getChanges(Model $model, array $cols);

// searches the history for a value in a column.

HistoryTracker::hasEverHad($modelId, string $colName, $value, string $tableName);

注意:所有查询都在事务内完成,以确保您不会得到不一致的数据。

最重要的方法是 getHistoryOf,它接受一个eloquent对象、要获取的列数组以及要计为变更的列数组。

$importantCols: 这是什么意思?!

考虑一个您有一个具有10列的表的情况,并且有2个表单来编辑列值。

例如,一个用于编辑 first namelast namebio 等的表单,另一个表单仅用于更改密码。

好,现在您需要显示第一个表单的提交历史。

这里您需要排除密码列,否则其他表单的提交将出现在第一个表单的历史中。

HistoryTracker::getChanges($user, ['first_name', 'last_name'], ['first_name', 'last_name', 'bio']);

这里我们不希望在表中显示bio,但我们想显示有关它的其他元数据,例如日期和用户名。

🙋 贡献

如果您发现问题或有更好的方法,请随时打开一个问题或拉取请求。

❗ 安全性

如果您发现任何与安全相关的问题,请使用 security tab 而不是使用问题跟踪器。

⭐ 您的星标让我们做得更多 ⭐

一如既往,如果您发现这个包很有用,并且希望鼓励我们维护和改进它,只需按星标按钮声明您的意愿即可。

作者的其他作品

Laravel Microscope

💎 它会自动在您的laravel应用程序中找到错误

Laravel HeyMan

💎 它允许您编写表达式的代码来授权、验证和验证。

I speak to everyone in the same way, whether he is the garbage man or the president of the university. 

"Albert Einstein"