stevebauman / revision
Eloquent 模型的修订版本。
该软件包的官方仓库似乎已丢失,因此该软件包已被冻结。
Requires
- php: >=5.5
- illuminate/database: 5.*
- illuminate/support: 5.*
Requires (Dev)
- orchestra/testbench: ~3.1
- phpspec/phpspec: ~2.1
- phpunit/phpunit: ~4.0
README
轻松跟踪优雅模型的所有更改。
安装
将修订版插入到您的 composer.json
文件中
"stevebauman/revision": "1.2.*"
现在运行 composer update
。完成之后,将修订版服务提供者插入到您的 config/app.php
文件中
Stevebauman\Revision\RevisionServiceProvider::class
运行 php vendor:publish
以发布修订版迁移文件。然后,运行 php artisan migrate
。
设置完成!
设置
创建 Revision
模型,并插入 belongsTo()
或 hasOne()
user()
关系以及 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); } }
将 Stevebauman\Revision\Traits\HasRevisionsTrait
插入到您想要跟踪更改的模型中
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(); } }
使用方法
修订列
您必须在模型中插入 $revisionColumns
属性以跟踪修订。
跟踪所有列
要跟踪模型数据库表上每个列的所有更改,请使用星号,如下所示
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 = ['*']; }
跟踪特定列
要跟踪特定列的更改,请插入您想要跟踪的列名,如下所示
class User extends Authenticatable { use Notifiable, HasRevisionsTrait; /** * The columns to keep revisions of. * * @var array */ protected $revisionColumns = [ 'user_id', 'title', 'description', ]; }
显示修订版本
要在记录上显示修订版本,请调用关系访问器 revisions
。记住,这只是一个常规的 Laravel 关系,因此您可以按需加载/懒加载修订版本
$post = User::with('revisions')->find(1); return view('user.show', ['user' => $user]);
在每次修订记录上,您可以使用以下方法来显示修订数据
getColumnName()
要显示修订发生的列名,请使用方法 getColumnName()
$revision = Revision::find(1); echo $revision->getColumnName(); // Returns string
getUserResponsible()
要检索执行修订的用户,请使用方法 getUserResponsible()
$revision = Revision::find(1); $user = $revision->getUserResponsible(); // Returns user model echo $user->id; echo $user->email; echo $user->first_name;
getOldValue()
要检索记录的旧值,请使用方法 getOldValue()
$revision = Revision::find(1); echo $revision->getOldValue(); // Returns string
getNewValue()
要检索记录的新值,请使用方法 getNewValue()
$revision = Revision::find(1); echo $revision->getNewValue(); // Returns string
示例
// 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
修改列名显示
要更改已修订的列名的显示,请将属性 $revisionColumnsFormatted
插入到模型中
/** * The formatted revised column names. * * @var array */ protected $revisionColumnsFormatted = [ 'user_id' => 'User', 'title' => 'Post Title', 'description' => 'Post Description', ];
修改值显示
要更改已修订的值的显示,请插入属性 $revisionColumnsMean
。您可以使用点表示法语法来表示关系值。例如
/** * The formatted revised column names. * * @var array */ protected $revisionColumnsMean = [ 'user_id' => 'user.full_name', ];
您甚至可以使用 Laravel 访问器与 revisionColumnsMean
属性一起使用。
注意:修订值将传递给访问器的第一个参数。
protected $revisionColumnsMean = [ 'status' => 'status_label', ]; public function getStatusLabelAttribute($status = null) { if(! $status) { $status = $this->getAttribute('status'); } return view('status.label', ['status' => $status])->render(); }