Blog Detail

13

Jul
A Version Control Package for Laravel Eloquent Models cover image

A Version Control Package for Laravel Eloquent Models

Version Control is a package that renders database version control for Eloquent Models. It also satisfies GxP compliance requirements. It operates by generating a separate *_versions database table that correlates with the model (i.e., users_versions).

Version control provides Foundations for making your database version controlled. It allows you to handle changes to files over time and save these modifications in a database. Also, you can easily access them whenever you need them. For example, if we manage user balance and update that balance we have no idea after updating that record that how can we get its old value. But now, we can get old values easily with the help of the version control package.

Installation

For the installation purpose, You've to run this command.

Composer require redsnapper/laravel-version-control

Next, you've to publish its assets by running this command.

php artisan publish:vendor 

Migrations

After that, You should set up your migrations, and you should use the given base Migration class to define migrations. We will create two tables for each model, one is the key (Normal) table, and the other is the (Version history) table.

For example:

use Redsnapper\LaravelVersionControl\Database\Blueprint;
use Redsnapper\LaravelVersionControl\Database\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $this->makeVcTables("users",function(Blueprint $table){
            $table->string('email')->unique();
            $table->string('password');
        },function(Blueprint $table){
            $table->string('email');
            $table->string('password');
        });
    }
}

Version Control Models

You've to extend the Redsnapper\LaravelVersionControl\Models\BaseModel Class for each model.

use Redsnapper\LaravelVersionControl\Models\BaseModel;

class Post extends BaseModel
{
    code goes here...
}

Versions relationship

In the end, you can locate versions from the models using the version's relationship.

$model->versions();

If you want to discover more about this package, you can get full installation instructions and source code on GitHub. RedSnapper/laravel-version-control

Published at : 13-07-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

web application development client
web application development client
web application development client
web application development client
web application development client

Launch project