Download the PHP package michaeljmeadows/has-histories without Composer
On this page you can find all versions of the php package michaeljmeadows/has-histories. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download michaeljmeadows/has-histories
More information about michaeljmeadows/has-histories
Files in michaeljmeadows/has-histories
Package has-histories
Short Description A simple trait to aid Eloquent model version history logging.
License MIT
Informations about the package has-histories
michaeljmeadows/has-histories
A simple trait to aid Eloquent model version history logging.
Installation
You can install the package via composer:
Usage
Add a migration to store your model histories. This should contain all the same fields as your main model table as well as a reference ID to the original model. Your model's history table can be named however your like, but the default convention would be models
-> model_histories
. We recommend modifying your migrations as shown in this modification of the Laravel Jetstream User migration:
Once the migration has been added, you can simply include the trait in your model's definition:
Restoring Models
Models can be restored using one of three methods:
These functions return true on success and false if a historic state was not found.
restorePrevious()
will restore a model to its previous state in the histories table.
restorePreviousIteration(int $index)
will restore a model to a state using zero-based numbering. (i.e. restorePreviousIteration(0)
is the same as restorePrevious()
).
restoreBeforeDate(DateTimeInterface|string $date)
will restore a model to the most recent state before the given $date
value using the updated_at
and created_at
fields.
Restoration Notes
- When restored, a copy of the current model is also saved to the histories table.
- HasHistories uses the history table
id
field to assess the most recent state, as multiple restorations can lead to apparent duplicates appearing in the histories table.
Customising Behaviour
Ignored Fields
Not every change to a model's attributes is worth logging. In the Jetstream User example above, it may be that you'd rather ignore changes to email_verified_at
. In this case, you can add a protected array attribute $ignoredFields
to your model specifying which attributes you're not interested in:
If you choose to ignore fields these should not be included in the history table migration.
Histories Table Name
By default, HasHistories uses the naming convention models
-> model_histories
when determining the history table name, but if for whatever reason that doesn't work for you, you can specify a different history table name by adding a protected string attribute $historiesTable
to your model:
Histories Table Model Reference
By default, HasHistories associates an entry in the history table with a model with an attribute in the form models
-> model_id
, but if for whatever reason that doesn't work for you, you can specify a different field by adding a protected string attribute $historiesModelIdReference
:
Histories Table Connection
By default, HasHistories expects that the history table will use the same connection as the model to which it is applied. Occasionally you may want to specify a different connection for your history table. This can be done with an optional parameter in your saveHistory
method call:
When restoring models, the same connection parameter can be added at the end of each method call:
All versions of has-histories with dependencies
illuminate/database Version ^8.0|^9.0|^10.0
illuminate/support Version ^8.0|^9.0|^10.0