Add Version Control to Laravel Models


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:

1use Redsnapper\LaravelVersionControl\Models\BaseModel;
3class Post extends BaseModel

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

1use Redsnapper\LaravelVersionControl\Database\Blueprint;
2use Redsnapper\LaravelVersionControl\Database\Migration;
4class CreateUsersTable extends Migration
6 /**
7 * Run the migrations.
8 *
9 * @return void
10 */
11 public function up()
12 {
13 $this->makeVcTables("users",function(Blueprint $table){
14 $table->string('email')->unique();
15 $table->string('password');
16 },function(Blueprint $table){
17 $table->string('email');
18 $table->string('password');
19 });
20 }

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


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.