Add Version Control to Laravel Models

Packages

July 9th, 2021

Add Version Control to Laravel Models

Version Control for Laravel is a package that provides database version control for Eloquent models. This package works by creating a separate *_versions database table that corresponds with the model (i.e., users_versions).

You start by extending the package's base model for models you'd like to include version control:

use Redsnapper\LaravelVersionControl\Models\BaseModel;
 
class Post extends BaseModel
{
}

Since two tables are required, you need to use the provided base Migration class to define migrations:

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');
});
}
}

Finally, you can retrieve model versions using the versions() method on models that extend the base model in this package:

$model->versions();

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.