All Projects → stevebauman → Revision

stevebauman / Revision

Revisions for Eloquent Models

Labels

Projects that are alternatives of or similar to Revision

Laravel User Settings
Simple and persistent boolean settings per user
Stars: ✭ 34 (-8.11%)
Mutual labels:  laravel
Laravel Qrcode Ecommerce
This is a complete laravel project that handles qrcodes, payments, api/microservices, and ecommerce
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Nuswhispers
NUSWhispers source.
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-5.41%)
Mutual labels:  laravel
Lynnhosting
Open Source, Web Hosting Automation built with Laravel
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Ys Tinify Laravel
Tinify API support with laravel
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Laravel Package Top 100
对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现在 2016 年 8 月 9号,有超过 7176 个扩展包,以下是下载量最大的 100 个。
Stars: ✭ 971 (+2524.32%)
Mutual labels:  laravel
Laravel Queue Database Ph4
Laravel Database Queue with Optimistic locking
Stars: ✭ 37 (+0%)
Mutual labels:  laravel
Nova Bar Metrics
A Laravel Nova tool for bar chart metrics and frequency distributions.
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Time Traveller
Time travel for your Laravel Models.
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Laravel Response Macros
Extra response macro's for Laravel
Stars: ✭ 35 (-5.41%)
Mutual labels:  laravel
Laravel Weather
🌤️ A wrapper around Open Weather Map API (Current weather)
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Laravel Support
Rinvex common support helpers, contracts, and traits required by various Rinvex packages. Validator functionality, and basic controller included out-of-the-box.
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Nideadmin
【未完成】NideAdmin - 基于 Vue.js + Egg.js 的微信小程序后台框架
Stars: ✭ 35 (-5.41%)
Mutual labels:  laravel
Laravel Ip Gateway
Laravel IP Gateway
Stars: ✭ 37 (+0%)
Mutual labels:  laravel
Nova Laravel Update Card
Check if you're running the latest Laravel version right from your Nova dashboard.
Stars: ✭ 34 (-8.11%)
Mutual labels:  laravel
Scrutiny
Ensure your laravel-based project is running in the correct server environment
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel
Mingyuyue
古诗词取名后台
Stars: ✭ 37 (+0%)
Mutual labels:  laravel
Stationery Cms
💡基于laravel-admin1.6.0开发的办公用品管理系统,含excel导出、查询快递功能
Stars: ✭ 37 (+0%)
Mutual labels:  laravel
Laravel Personality Insights
👩 👱 Using IBM Watson Personality Insights Service with Laravel 5
Stars: ✭ 36 (-2.7%)
Mutual labels:  laravel

Revision

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();
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].