{"id":13563859,"url":"https://github.com/overtrue/laravel-versionable","last_synced_at":"2025-05-14T08:06:12.242Z","repository":{"id":34913889,"uuid":"189577776","full_name":"overtrue/laravel-versionable","owner":"overtrue","description":"⏱️Make Laravel model versionable","archived":false,"fork":false,"pushed_at":"2025-02-24T22:51:25.000Z","size":178,"stargazers_count":552,"open_issues_count":11,"forks_count":47,"subscribers_count":10,"default_branch":"5.x","last_synced_at":"2025-05-11T04:09:52.872Z","etag":null,"topics":["laravel-model-versionable","laravel-package","laravel-reversion","model-versioning"],"latest_commit_sha":null,"homepage":"","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/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["overtrue"]}},"created_at":"2019-05-31T10:44:51.000Z","updated_at":"2025-05-09T19:36:01.000Z","dependencies_parsed_at":"2024-11-28T02:02:07.398Z","dependency_job_id":"bf1b7a2d-8d6d-4c54-96ba-02dde6e8fc81","html_url":"https://github.com/overtrue/laravel-versionable","commit_stats":{"total_commits":141,"total_committers":29,"mean_commits":4.862068965517241,"dds":"0.36170212765957444","last_synced_commit":"aad410e419a43ae6ac2752b0b69b2c60718e6d2b"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-versionable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-versionable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-versionable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-versionable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-versionable/tar.gz/refs/heads/5.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101615,"owners_count":22014909,"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","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":["laravel-model-versionable","laravel-package","laravel-reversion","model-versioning"],"created_at":"2024-08-01T13:01:24.019Z","updated_at":"2025-05-14T08:06:07.228Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Versionable\n\n[![Build Status](https://github.com/overtrue/laravel-versionable/workflows/CI/badge.svg)](https://github.com/overtrue/laravel-versionable/actions)\n[![Latest Stable Version](https://poser.pugx.org/overtrue/laravel-versionable/v/stable.svg)](https://packagist.org/packages/overtrue/laravel-versionable)\n[![Latest Unstable Version](https://poser.pugx.org/overtrue/laravel-versionable/v/unstable.svg)](https://packagist.org/packages/overtrue/laravel-versionable)\n[![Total Downloads](https://poser.pugx.org/overtrue/laravel-versionable/downloads)](https://packagist.org/packages/overtrue/laravel-versionable)\n[![License](https://poser.pugx.org/overtrue/laravel-versionable/license)](https://packagist.org/packages/overtrue/laravel-versionable)\n\nIt's a minimalist way to make your model support version history, and it's very simple to revert to the specified\nversion.\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n## Requirement\n\n1. PHP \u003e= 8.1.0\n2. laravel/framework \u003e= 9.0\n\n## Features\n\n- Keep the specified number of versions.\n- Whitelist and blacklist for versionable attributes.\n- Easily revert to the specified version.\n- Record only changed attributes.\n- Easy to customize.\n\n## Installing\n\n```shell\ncomposer require overtrue/laravel-versionable -vvv\n```\n\nFirst, publish the config file and migrations:\n\n```bash\nphp artisan vendor:publish --provider=\"Overtrue\\LaravelVersionable\\ServiceProvider\"\n```\n\nThen run this command to create a database migration:\n\n```bash\nphp artisan migrate\n```\n\n## Usage\n\nAdd `Overtrue\\LaravelVersionable\\Versionable` trait to the model and set versionable attributes:\n\n```php\nuse Overtrue\\LaravelVersionable\\Versionable;\n\nclass Post extends Model\n{\n    use Versionable;\n\n    /**\n     * Versionable attributes\n     *\n     * @var array\n     */\n    protected $versionable = ['title', 'content'];\n\n    // Or use a blacklist\n    //protected $dontVersionable = ['created_at', 'updated_at'];\n\n    \u003c...\u003e\n}\n```\n\nVersions will be created on the vensionable model saved.\n\n```php\n$post = Post::create(['title' =\u003e 'version1', 'content' =\u003e 'version1 content']);\n$post-\u003eupdate(['title' =\u003e 'version2']);\n```\n\n### Get versions\n\n```php\n$post-\u003eversions; // all versions\n$post-\u003elatestVersion; // latest version\n// or\n$post-\u003elastVersion;\n\n$post-\u003eversions-\u003efirst(); // first version\n// or\n$post-\u003efirstVersion;\n\n$post-\u003eversionAt('2022-10-06 12:00:00'); // get version from a specific time\n// or\n$post-\u003eversionAt(\\Carbon\\Carbon::create(2022, 10, 6, 12));\n```\n\n### Revert\n\nRevert a model instance to the specified version:\n\n```php\n$post-\u003egetVersion(3)-\u003erevert();\n\n// or\n\n$post-\u003erevertToVersion(3);\n```\n\n#### Revert without saving\n\n```php\n$version = $post-\u003eversions()-\u003efirst();\n\n$post = $version-\u003erevertWithoutSaving();\n```\n\n### Remove versions\n\n```php\n// soft delete\n$post-\u003eremoveVersion($versionId = 1);\n$post-\u003eremoveVersions($versionIds = [1, 2, 3]);\n$post-\u003eremoveAllVersions();\n\n// force delete\n$post-\u003eforceRemoveVersion($versionId = 1);\n$post-\u003eforceRemoveVersions($versionIds = [1, 2, 3]);\n$post-\u003eforceRemoveAllVersions();\n```\n\n### Restore deleted version by id\n\n```php\n$post-\u003erestoreTrashedVersion($id);\n```\n\n### Temporarily disable versioning\n\n```php\n// create\nPost::withoutVersion(function () use (\u0026$post) {\n    Post::create(['title' =\u003e 'version1', 'content' =\u003e 'version1 content']);\n});\n\n// update\nPost::withoutVersion(function () use ($post) {\n    $post-\u003eupdate(['title' =\u003e 'updated']);\n});\n```\n\n### Custom Version Store strategy\n\nYou can set the following different version policies through property `protected $versionStrategy`:\n\n- `Overtrue\\LaravelVersionable\\VersionStrategy::DIFF` - Version content will only contain changed attributes (default\n  strategy).\n- `Overtrue\\LaravelVersionable\\VersionStrategy::SNAPSHOT` - Version content will contain all versionable attribute\n  values.\n\n### Show diff between the two versions\n\n```php\n$diff = $post-\u003egetVersion(1)-\u003ediff($post-\u003egetVersion(2));\n```\n\n`$diff` is a object `Overtrue\\LaravelVersionable\\Diff`, it based\non [jfcherng/php-diff](https://github.com/jfcherng/php-diff).\n\nYou can render the diff to [many formats](https://github.com/jfcherng/php-diff#introduction), and all formats result\nwill be like follows:\n\n```php\n[\n    $attribute1 =\u003e $diffOfAttribute1,\n    $attribute2 =\u003e $diffOfAttribute2,\n    ...\n    $attributeN =\u003e $diffOfAttributeN,\n]\n```\n\n#### toArray()\n\n```php\n$diff-\u003etoArray();\n//\n[\n    \"name\" =\u003e [\n        \"old\" =\u003e \"John\",\n        \"new\" =\u003e \"Doe\",\n    ],\n    \"age\" =\u003e [\n        \"old\" =\u003e 25,\n        \"new\" =\u003e 26,\n    ],\n]\n```\n\n### Other formats\n\n```php\ntoArray(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoJsonText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoContextText(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoInlineHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoJsonHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\ntoSideBySideHtml(array $differOptions = [], array $renderOptions = [], bool $stripTags = false): array\n```\n\n\u003e **Note**\n\u003e\n\u003e `$differOptions` and `$renderOptions` are optional, you can set them following the README\n\u003e of [jfcherng/php-diff](https://github.com/jfcherng/php-diff#example).\n\u003e `$stripTags` allows you to remove HTML tags from the Diff, helpful when you don't want to show tags.\n\n### Using custom version model\n\nYou can define `$versionModel` in a model, that used this trait to change the model(table) for versions\n\n\u003e **Note**\n\u003e\n\u003e Model MUST extend class `\\Overtrue\\LaravelVersionable\\Version`;\n\n```php\n\u003c?php\n\nclass PostVersion extends \\Overtrue\\LaravelVersionable\\Version\n{\n    //\n}\n```\n\nUpdate the model attribute `$versionModel`:\n\n```php\n\u003c?php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Overtrue\\LaravelVersionable\\Versionable;\n\nclass Post extends Model\n{\n    use Versionable;\n\n    public string $versionModel = PostVersion::class;\n}\n```\n\n## Intergrations\n\n- [mansoorkhan96/filament-versionable](https://github.com/mansoorkhan96/filament-versionable) Effortlessly manage revisions of your Eloquent models in Filament.\n\n## :heart: Sponsor me\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n## Contributing\n\nYou can contribute in one of three ways:\n\n1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-versionable/issues).\n2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-versionable/issues).\n3. Contribute new features or update the wiki.\n\n_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and\nPSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-versionable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-versionable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-versionable/lists"}