debuqer / eloquent-memory
Let the eloquent remember its current state of data
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
Conflicts
- phpunit/phpunit: <10.0.17
This package is auto-updated.
Last update: 2025-01-07 12:44:02 UTC
README
Eloquent memory give you a Laravel model based time machine to perform time traveling through your models state.
$article = Article::create([ 'name' => 'Women, Life, Freedom', 'content' => 'Hey this content just added,', ]); // 5 minutes later // let's change the content $article->update([ 'content' => 'Hey this content just changed,' ]); // // Oops, we have changed our content by mistake, let's go back $articleBeforeUpdate = $article->getStatyeOf(Carbon::now()->subMinutes(2)); $articleBeforeUpdate->save(); // will rollback the content of article to the 1 minute ago
Installation
You can install the package via composer:
composer require debuqer/eloquent-memory
You can publish and run the migrations with:
php artisan vendor:publish --tag="eloquent-memory-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="eloquent-memory-config"
This is the contents of the published config file:
return [ 'changes' => [ 'model-updated' => ModelUpdated::class, 'model-created' => ModelCreated::class, 'model-deleted' => ModelDeleted::class, ], 'drivers' => [ 'default' => 'eloquent', 'eloquent' => [ 'class_name' => \Debuqer\EloquentMemory\Repositories\Eloquent\EloquentTransitionPersistDriver::class, 'connection' => 'default', ], ], ];
Usage
In order to force models to keep track of their states, CanRememberStates trait must be used in the model class
use Debuqer\EloquentMemory\CanRememberStates; class Post extends Model { use CanRememberStates; }
The model records their states in a database and the states can be retrieved by method getStateOf
$oldArticle = Article::find(5)->getStateOf(Carbon::now()->subMinutes(5));
Road map
-
Fix migration, so the subject_key should be provide as an array
-
Fix migration to be publishable via laravel
-
no date_recorded field are stored
Testing
composer test
Changelog
This package is in dev mode and not recommend to use in production environment
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.