{"id":33203169,"url":"https://github.com/sbarre/eloquent-versioned","last_synced_at":"2025-12-12T22:43:22.893Z","repository":{"id":32594869,"uuid":"36178296","full_name":"sbarre/eloquent-versioned","owner":"sbarre","description":"Add transparent versioning to Laravel 5's Eloquent ORM","archived":false,"fork":false,"pushed_at":"2016-05-24T16:48:39.000Z","size":75,"stargazers_count":30,"open_issues_count":9,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-07-23T22:32:09.325Z","etag":null,"topics":["eloquent","laravel","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sbarre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-24T15:19:13.000Z","updated_at":"2022-06-21T09:48:55.000Z","dependencies_parsed_at":"2022-09-15T12:22:04.418Z","dependency_job_id":null,"html_url":"https://github.com/sbarre/eloquent-versioned","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/sbarre/eloquent-versioned","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbarre%2Feloquent-versioned","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbarre%2Feloquent-versioned/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbarre%2Feloquent-versioned/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbarre%2Feloquent-versioned/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sbarre","download_url":"https://codeload.github.com/sbarre/eloquent-versioned/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbarre%2Feloquent-versioned/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":284813088,"owners_count":27067232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-17T02:00:06.431Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","php"],"created_at":"2025-11-16T09:00:32.855Z","updated_at":"2025-11-17T03:00:52.528Z","avatar_url":"https://github.com/sbarre.png","language":"PHP","funding_links":[],"categories":["Packages"],"sub_categories":["Database/Eloquent/Models"],"readme":"# Eloquent Versioned\n\nAdds transparent versioning support to Laravel 5.2's Eloquent ORM.\n\n**WARNING: This repository is currently super-duper experimental.  I will gladly accept pull requests and issues, but you probably shouldn't use this in production, and the interfaces may change without notice (although major changes will bump the version).**\n\n**It was also recently updated to bring global scopes in line with Laravel 5.2\nso if you are not yet on 5.2, stick with release 0.0.7.**\n\nWhen using this trait (and with a table that includes the required fields), saving your model will actually create a new row instead, and increment the version number.\n\nUsing global scopes, old versions are ignored in the standard ORM operations (selects, updates, deletes) and relations (hasOne, hasMany, belongsTo, etc).\n\nThe package also provides some special methods to include old versions in queries (or only query old versions) which can be useful for showing a model's history, or the like.\n\n## Installation\n\nTo add via Composer:\n\n```\ncomposer require sbarre/eloquent-versioned --no-dev\n```\n\nUse the `--no-dev` flag to avoid pulling down all the testing dependencies (like the *entire Laravel framework*).\n\n## Migrations\n\nVersioned models require that your database table contain 3 fields to handle the versioning.\n\nIf you are creating a new table, or if you are changing an existing table, include the following lines in the `up()` method of the migration:\n\n```php\n$table-\u003einteger('model_id')-\u003eunsigned()-\u003edefault(1);\n$table-\u003einteger('version')-\u003eunsigned()-\u003edefault(1);\n$table-\u003einteger('is_current_version')-\u003eunsigned()-\u003edefault(1);\n$table-\u003eindex('is_current_version');\n$table-\u003eindex('model_id');\n$table-\u003eindex('version');\n```\n\nIf your migration was altering an existing table, you should include these lines in the `down()` method of your migration:\n\n```php\n$table-\u003edropColumn(['model_id','version','is_current_version']);\n$table-\u003edropIndex(['model_id','version','is_current_version']);\n```\n\n#### Caveats\n\nIf you change the constants in `EloquentVersioned\\VersionedBuilder` to rename the columns, remember to change them in your migrations as well.\n\n## Usage\n\nIn your Eloquent model class, start by adding the `use` statement for the Trait:\n\n```php\nuse EloquentVersioned\\Traits\\Versioned;\n```\n\nWhen the trait boots it will apply the proper scope, and provides overrides on various Eloquent methods to support versioned records.\n\nOnce the trait is applied, you use your models as usual, with the standard queries behaving as usual.\n\n```php\n$project = Project::create([\n    'name' =\u003e 'Project Name',\n    'description' =\u003e 'Project description goes here'\n])-\u003efresh();\n\nprint_r($project-\u003etoArray());\n```\n\nThis would then output (for example):\n\n```php\nArray\n(\n    [id] =\u003e 1\n    [version] =\u003e 1\n    [name] =\u003e Project Name\n    [description] =\u003e Project description goes here\n    [created_at] =\u003e 2015-05-24 17:16:05\n    [updated_at] =\u003e 2015-05-24 17:16:05\n)\n```\n\nThe actual database row looks like this:\n\n```php\nArray\n(\n    [id] =\u003e 1\n    [model_id] =\u003e 1\n    [version] =\u003e 1\n    [is_current_version] =\u003e 1\n    [name] =\u003e Updated project Name\n    [description] =\u003e Project description goes here\n    [created_at] =\u003e 2015-05-24 17:16:05\n    [updated_at] =\u003e 2015-05-24 17:16:05\n)\n```\n\nThen if you change the model and save:\n\n```php\n$project-\u003ename = 'Updated project name';\n$project-\u003esave();\n\nprint_r($project-\u003etoArray());\n```\nThis would then output:\n\n```php\nArray\n(\n    [id] =\u003e 1\n    [version] =\u003e 2\n    [name] =\u003e Updated project Name\n    [description] =\u003e Project description goes here\n    [created_at] =\u003e 2015-05-24 17:16:05\n    [updated_at] =\u003e 2015-05-24 17:16:45\n)\n```\n\nThe model mutates the `model_id` column into `id`, and hides some of the version-specific columns.  In reality this is actually the same database row that now looks like this:\n\n```php\nArray\n(\n    [id] =\u003e 1\n    [model_id] =\u003e 1\n    [version] =\u003e 2\n    [is_current_version] =\u003e 1\n    [name] =\u003e Updated project Name\n    [description] =\u003e Project description goes here\n    [created_at] =\u003e 2015-05-24 17:16:05\n    [updated_at] =\u003e 2015-05-24 17:16:45\n)\n```\n\nWhile a new row is inserted to save our previous version, which now looks like this:\n\n```php\nArray\n(\n    [id] =\u003e 2\n    [model_id] =\u003e 1\n    [version] =\u003e 1\n    [is_current_version] =\u003e 0\n    [name] =\u003e Project Name\n    [description] =\u003e Project description goes here\n    [created_at] =\u003e 2015-05-24 17:16:05\n    [updated_at] =\u003e 2015-05-24 17:16:05\n)\n```\n\nSo the `is_current_version` property is what the global scope is applied against, limiting all select queries to only records where `is_current_version = 1`.\n\nCalling `save()` on a model replicates the original version into a new row (with `is_current_version = 0`), then increments the `version_id` property on our current model, changes the appropriate timestamps, and saves it.\n\nIf you are making a very minor change to a model and you don't want to create a new version, you can call `saveMinor()` instead.\n\n```php\n$project-\u003esaveMinor(); // doesn't create a new version\n```\n\n#### Methods for dealing with old versions\n\nIf you want to retrieve a list of all versions of a model (or include old versions in a bigger query):\n\n```php\n$projectVersions = Project::withOldVersions()-\u003efind(1);\n```\n\nIf run after our example above, this would return an array with 2 models.\n\nYou can also retrieve a list of *only* old models by using:\n\n```php\n$oldVersions = Project::onlyOldVersions()-\u003efind(1);\n```\n\nOtherwise, the rest of Eloquent's ORM operations should work as usual, including the out-of-the-box relations.\n\n#### Methods for moving through a model's versions\n\nIf you want to navigate through all of model's versions, in a linked-list manner:\n\n```php\n$current = Project::find(1);\n\n$previous = $current-\u003egetPreviousModel();\n$next = $previous-\u003egetNextModel();\n\n// $next == $current\n```\n\nIf you are at the most recent version, `getNextModel()` will return `null` and likewise if you are at the oldest version, `getPreviousModel()` will return `null`.\n\n## Support \u0026 Roadmap\n\nAs indicated at the top, this package is still **very experimental** and is under active development.  The current roadmap includes test coverage and more extensive real-world testing, so pull requests and issues are always welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbarre%2Feloquent-versioned","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsbarre%2Feloquent-versioned","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbarre%2Feloquent-versioned/lists"}