fico7489/laravel-revisionable-upgrade

为 venturecraft revisionable 包提供升级,添加了许多有用方法。

3.0.4 2020-09-28 08:31 UTC

This package is auto-updated.

Last update: 2024-08-28 17:46:16 UTC


README

Venturecraft Revisionable 包提供升级,添加了许多有用方法。

为什么使用

是的,可修订包有 userResponsible() 方法,但这还不够,我们可以使用保存的修订来执行更多有用的事情。我们可以找出谁创建了、删除了和编辑了模型,模型何时被编辑,模型的确切属性何时被编辑等等。

  • 你不需要在你的表中添加 updated_bydeleted_bycreated_by
  • 你不需要在你的表中添加 updated_attribute_by
  • 你不需要在你的表中添加 updated_attribute_to_value_by
  • 你不需要在你的表中添加 updated_attribute_from_value_to_value_by
  • 你不需要在你的表中添加 updated_attribute_at
  • 你不需要在你的表中添加 updated_attribute_to_value_at
  • 你不需要在你的表中添加 updated_attribute_from_value_to_value_at
  • 你不需要 author_idcreated_user_iddeleted_user_id 等等或类似的东西。

不要在数据库表中添加上述表列,所有这些信息都已存储在修订表中,我们只需要获取它们的能力,这个包将帮助你实现这一点。

版本兼容性

该包适用于 laravel 5.* 版本

安装

1. 使用 composer 安装包

composer require fico7489/laravel-revisionable-upgrade:"*"

2. 在你的基础模型或特定模型中使用 Fico7489\Laravel\RevisionableUpgrade\Traits\RevisionableUpgradeTrait 特性。使用 RevisionableUpgradeTrait 的模型必须也使用 RevisionableTrait;

...
use Venturecraft\Revisionable\RevisionableTrait;
use Fico7489\Laravel\RevisionableUpgrade\Traits\RevisionableUpgradeTrait;

abstract class BaseModel extends Model
{
    use RevisionableTrait;
    use RevisionableUpgradeTrait;
    
    //enable this if you want use methods that gets information about creating
    protected $revisionCreationsEnabled = true;
...

就这样,你就可以开始了。

新方法

  • userCreated() 返回创建此模型的用户

  • userDeleted() 返回删除此模型的用户

  • userUpdated($key = null, $newValue = null, $oldValue = null) 返回更新此模型的用户(如果有多个,则是最后一个用户)

  • revisionCreated() 返回创建的模型修订

  • revisionDeleted() 返回删除的模型修订

  • revisionUpdated($key = null, $newValue = null, $oldValue = null) 返回更新的模型修订(如果有多个,则是最后一个修订)

  • dateUpdated($key = null, $newValue = null, $oldValue = null) 返回更新模型的时间(如果有多个,则是最后一个修订的时间)

  • revisionsUpdated($key = null, $newValue = null, $oldValue = null) 返回更新的模型修订

  • usersUpdated($key = null, $newValue = null, $oldValue = null) 返回更新的模型用户

关于具有 ($key = null, $newValue = null, $oldValue = null) 的方法的说明

  • 所有参数都是可选的
  • 如果你提供 $key,方法将查找该键/字段的更改
  • 如果你提供 $newValue,方法将查找键/字段更改为该值的更改
  • 如果你提供 $oldValue,方法将查找键/字段从该值更改的更改

我们不需要 dateCreateddateDeleted,因为这些信息存储在 created_at 和 deleted_at 中。我们不需要 revisionsCreatedrevisionsDeletedusersCreatedusersDeleted,因为模型只能创建或删除一次。返回 userusers 的方法使用 auth.model 配置中的模型。

看看效果

$seller = Seller::create(['email' => 'test@test.com']);

//get user who edited model
$seller->userCreated();

//get user who deleted model
$seller->userDeleted();

//get user who updated model
$seller->userUpdated();

//get user who updated attribute name in model
$seller->userUpdated('name');

//get user who updated attribute name value to 'new_name'
$seller->userUpdated('name', 'new_name');

//get user who updated attribute name value to 'new_name' value from 'old_name' value
$seller->userUpdated('name', 'new_name',  'old_name');

//get revision model for create
$seller->revisionCreated();

//get revision model for delete
$seller->revisionDeleted();

//get revision model for update
$seller->revisionUpdated();

//get date for update
$seller->dateUpdated();

//get revisions for update
$seller->revisionsUpdated();

//get users for update
$seller->usersUpdated();

重要说明

你想要使用此包的模型必须使用 created_at 时间戳

如果您想检索已删除模型的用户,必须启用$revisionCreationsEnabled

protected $revisionCreationsEnabled = true;

查看更多

许可协议

MIT

自由软件,太棒了!