Laravel Time Traveller
Time travelling for Laravel 5 models.
- Uses database table to store model states.
- Can be accessed from ORM or URL Query String.
- Bundled command to keep revisions table healthy.
- Ability to override Revisions model for extension., (*1)
Installation
Run the following command on your project root.
composer require conceptbyte/time-traveller
, (*2)
Add the service provider to the config/app.php
file under the providers
key. Make sure to add it after the default laravel service providers., (*3)
'providers' => [
...,
'ConceptByte\TimeTraveller\TimeTravellerServiceProvider'
]
Publish Configuration
Run php artisan vendor:publish
to publish the package configurations., (*4)
Modify Configuration
Use the config/timetraveller.php
file to modify the package defaults.
- Revisions Model 'model' => ConceptByte\TimeTraveller\Models\Revision::class
- Query String Parameter 'at' => 'at'
- Clear audits that are older than 'clear' => '365'
, (*5)
Usage
Enable time traveller on a model by using the trait., (*6)
class Post extents Model
{
use TimeTravel;
public function getBy()
{
return Auth::user()->name;
}
}
All models that use the trait must implement abstract public function getBy()
which returns any string.
This function can be used to save any additional attributes such as the owner of the change., (*7)
Get the state of a record at a specific data/time.
Post::at('58781813')->find(1);
Get the state of a record using a query string.
URL: timetravel.app/posts/1?at=58781813
, (*8)
Post::find(1);
Get a model with revisions
Post::with('revisions')->first();
You can clear the audits table records that are older than a specified range.
php artisan time-traveller:clear
. This will read the config file and clear records that are older than the configured number of days., (*9)