aaronschmied/laravel-model-history

该包已被废弃且不再维护。未建议替代包。

用于Laravel接收电子邮件的SMTP服务器。

1.0.1 2019-10-17 23:26 UTC

This package is auto-updated.

Last update: 2022-07-18 05:34:40 UTC


README

记录对eloquent模型所做的更改。

安装

composer require aaronschmied/laravel-model-history

包将自动发现。

要发布迁移文件,运行以下Artisan命令

php artisan vendor:publish --provider="AaronSchmied\ModelHistory\Providers\ModelHistoryServiceProvider" --tag="migrations"

要更改配置,请使用以下命令发布它:(此步骤为可选)

php artisan vendor:publish --provider="AaronSchmied\ModelHistory\Providers\ModelHistoryServiceProvider" --tag="config"

用法

将特质添加到您希望记录更改的模型类中

use AaronSchmied\ModelHistory\Traits\RecordsChanges;
use Illuminate\Database\Eloquent\Model;

class Example extends Model {
    use RecordsChanges;
}

您的模型现在与所做的所有更改相关联

$example->changes->last();

AaronSchmied\ModelHistory\Change {
  #attributes: array:8 [
    ...
    "change_type" => "updated"
    "changes" => "{
        "before": {
            "body": "Some old content"
        },
        "after": {
            "body": "This is the new body"
        }
    }"
    "recorded_at" => "2019-06-21 23:31:15"
  ]
  ...
}

更改模型还包括对执行更改的用户以及记录时间戳的关联。

您可以使用查询作用域来筛选更改

// Get the updates on the given model, by the given user, in the last 30 days:
Change::query()
    ->whereAuthor($user)
    ->whereSubject($model)
    ->whereType(Change::TYPE_UPDATED)
    ->whereRecordedBetween(now()->subDays(30), now())
    ->get();