承接 stevebauman/revision 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stevebauman/revision

最新稳定版本:v1.2.1

Composer 安装命令:

composer require stevebauman/revision

包简介

Revisions for Eloquent Models.

README 文档

README

Travis CI Scrutinizer Code Quality Latest Stable Version Total Downloads License

Easily track any changes to an eloquent model.

Installation

Insert Revision in your composer.json file:

"stevebauman/revision": "1.2.*"

Now run composer update. Once that's complete, insert the Revision service provider inside your config/app.php file:

Stevebauman\Revision\RevisionServiceProvider::class

Run php vendor:publish to publish the Revision migration file. Then, run php artisan migrate.

You're all set!

Setup

Create the Revision model and insert the belongsTo() or hasOne() user() relationship as well as the RevisionTrait:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Stevebauman\Revision\Traits\RevisionTrait;

class Revision extends Model
{
    use RevisionTrait;
    
    /**
     * The revisions table.
     *
     * @var string
     */
    protected $table = 'revisions';
    
    /**
     * The belongs to user relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(App\User::class);
    }
}

Insert the Stevebauman\Revision\Traits\HasRevisionsTrait onto your model that you'd like to track changes on:

namespace App;

use Stevebauman\Revision\Traits\HasRevisionsTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasRevisionsTrait;
    
    /**
     * The morphMany revisions relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function revisions()
    {
        return $this->morphMany(App\Revision::class, 'revisionable');
    }
    
    /**
     * The current users ID for storage in revisions.
     *
     * @return int|string
     */
    public function revisionUserId()
    {
        return auth()->id();
    }
}

Usage

Revision Columns

You must insert the $revisionColumns property on your model to track revisions.

Tracking All Columns

To track all changes on every column on the models database table, use an asterisk like so:

use Stevebauman\Revision\Traits\HasRevisionsTrait;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasRevisionsTrait;
    
    /**
     * The columns to keep revisions of.
     *
     * @var array
     */
    protected $revisionColumns = ['*'];
}
Tracking Specific Columns

To track changes on specific columns, insert the column names you'd like to track like so:

class User extends Authenticatable
{
    use Notifiable, HasRevisionsTrait;
    
    /**
     * The columns to keep revisions of.
     *
     * @var array
     */
    protected $revisionColumns = [
        'user_id',
        'title', 
        'description',
    ];
}

Displaying Revisions

To display your revisions on a record, call the relationship accessor revisions. Remember, this is just a regular Laravel relationship, so you can eager load / lazy load your revisions as you please:

$post = User::with('revisions')->find(1);

return view('user.show', ['user' => $user]);

On each revision record, you can use the following methods to display the revised data:

getColumnName()

To display the column name that the revision took place on, use the method getColumnName():

$revision = Revision::find(1);

echo $revision->getColumnName(); // Returns string
getUserResponsible()

To retrieve the User that performed the revision, use the method getUserResponsible():

$revision = Revision::find(1);

$user = $revision->getUserResponsible(); // Returns user model

echo $user->id;
echo $user->email;
echo $user->first_name;
getOldValue()

To retrieve the old value of the record, use the method getOldValue():

$revision = Revision::find(1);

echo $revision->getOldValue(); // Returns string
getNewValue()

To retrieve the new value of the record, use the method getNewValue():

$revision = Revision::find(1);

echo $revision->getNewValue(); // Returns string
Example
// In your `post.show` view:

@if($post->revisions->count() > 0)
    
     <table class="table table-striped">
     
        <thead>
            <tr>
                <th>User Responsible</th>
                <th>Changed</th>
                <th>From</th>
                <th>To</th>
                <th>On</th>
            </tr>
        </thead>
        
        <tbody>
        
        @foreach($post->revisions as $revision)
        
            <tr>
            
                <td>
                    {{ $revision->getUserResponsible()->first_name }}
                    
                    {{ $record->getUserResponsible()->last_name }}
                </td>
                
                <td>{{ $revision->getColumnName() }}</td>
                
                <td>
                    @if(is_null($revision->getOldValue()))
                        <em>None</em>
                    @else
                        {{ $revision->getOldValue() }}
                    @endif
                </td>
                
                <td>{{ $revision->getNewValue() }}</td>
                
                <td>{{ $revision->created_at }}</td>
                
            </tr>
            
        @endforeach
        
        </tbody>
            
    </table>
@else
    <h5>There are no revisions to display.</h5>
@endif

Modifying the display of column names

To change the display of your column name that has been revised, insert the property $revisionColumnsFormatted on your model:

/**
 * The formatted revised column names.
 *
 * @var array
 */
protected $revisionColumnsFormatted = [
    'user_id' => 'User',
    'title' => 'Post Title',
    'description' => 'Post Description',
];

Modifying the display of values

To change the display of your values that have been revised, insert the property $revisionColumnsMean. You can use dot notation syntax to indicate relationship values. For example:

/**
 * The formatted revised column names.
 *
 * @var array
 */
protected $revisionColumnsMean = [
    'user_id' => 'user.full_name',
];

You can even use laravel accessors with the revisionColumnsMean property.

Note: The revised value will be passed into the first parameter of the accessor.

protected $revisionColumnsMean = [
    'status' => 'status_label',
];

public function getStatusLabelAttribute($status = null)
{
    if(! $status) {
        $status = $this->getAttribute('status');
    }
    
    return view('status.label', ['status' => $status])->render();
}

stevebauman/revision 适用场景与选型建议

stevebauman/revision 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.91k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2015 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stevebauman/revision 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stevebauman/revision 我们能提供哪些服务?
定制开发 / 二次开发

基于 stevebauman/revision 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 4
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-12