Revisionable
repository·master·Indexed 25 days ago
https://github.com/venturecraft/revisionableA Laravel package for maintaining revision history for Eloquent models. It tracks changes to model attributes, including who made the change and when, to provide accountability and audit trails. Features include the RevisionableTrait for easy implementation, configurable history limits, field-level tracking control, and a FieldFormatter for human-readable output of boolean, datetime, and option values.
What's inside venturecraft-revisionable
- Run the artisan vendor:publish command to bring the configuration and migration files into your project, then run the migrations.
Install Revisionable via Composer
masterAdd
venturecraft/revisionableto yourcomposer.jsonfile and run the update command to install the package."venturecraft/revisionable": "1.*"php composer.phar updateRegister Revisionable Service Provider (Laravel 5.x)
masterFor Laravel 5.x, manually register the
RevisionableServiceProviderin yourconfig/app.phpfile.'providers' => [ Venturecraft\Revisionable\RevisionableServiceProvider::class, ]Implement Revisionable using the recommended Trait
masterTo enable revision history for a model, use the
RevisionableTraitwithin your model class. This works with standard Eloquent models or any class extending Eloquent.namespace App; use \Venturecraft\Revisionable\RevisionableTrait; class Article extends \Illuminate\Database\ Eloquent\Model { use RevisionableTrait; }Format revision output values
masterYou can control how field values are formatted in the revision history by defining the
$revisionFormattedFieldsarray in your model. This is useful for converting booleans to text, formatting dates, or wrapping strings in HTML.Supported formatters:
string:<format>: Uses%sas a placeholder for the value (e.g.,string:<strong>%s</strong>).boolean:false_value|true_value: Maps 0/1 to custom text (e.g.,boolean:No|Yes).options:key.value|key.value: Maps specific values to custom text (e.g.,options:search.On|network.In).datetime:format: Uses standard PHP datetime formatting (e.g.,datetime:m/d/Y g:i A).isEmpty:empty_value|non_empty_value: Checks if a value is null or an empty string. Supports%sfor the non-empty value (e.g.,isEmpty:Nothing|%s).
protected $revisionFormattedFields = [ 'title' => 'string:<strong>%s</strong>', 'public' => 'boolean:No|Yes', 'modified' => 'datetime:m/d/Y g:i A', 'deleted_at' => 'isEmpty:Active|Deleted' ];Store additional metadata in revisions
masterTo include extra model fields (likeaccount_id) in your revisions, first add the columns to your revision table via a custom migration. Then, register the field names inconfig/revisionable.phpunder theadditional_fieldskey. Ensure these columns arenullable()in your migrations if they aren't present in every model.Configure strings for null or unknown foreign keys
masterIf a revision contains a foreign key that no longer exists or was previously null, you can customize the text displayed in the history by setting these properties in your model:
$revisionNullString: String used when the value was null.$revisionUnknownString: String used when the foreign key is no longer found in the database.
protected $revisionNullString = 'nothing'; protected $revisionUnknownString = 'unknown';Override revision field names
masterTo change the display name of a field when calling
$revision->fieldName(), use the$revisionFormattedFieldNamesarray in your model. This is particularly useful for making database column names (likesmall_name) more human-readable (likeNickname).protected $revisionFormattedFieldNames = [ 'title' => 'Title', 'small_name' => 'Nickname', 'deleted_at' => 'Deleted At' ];Implement identifiableName() for foreign keys
masterBy default,
oldValue()andnewValue()return the ID when dealing with foreign keys. To display a meaningful name (like a title or name) instead of an ID in your revision history, override theidentifiableName()method in your model.use Venturecraft\ Revisionable\nRevisionable; class Article extends Revisionable { public function identifiableName() { return $this->title; } }Control which fields are tracked for revisions
masterYou can restrict revision tracking to specific fields or exclude certain fields. Note that$keepRevisionOftakes precedence over$dontKeepRevisionOf.Display revision history details
masterWhen iterating through
$model->revisionHistory, use the following methods to build human-readable change logs:$history->userResponsible(): Returns the User model responsible for the change, orfalseif no user is recorded. The user model is determined by yourconfig/auth.phpsettings.$history->fieldName(): Returns the name of the field updated. For foreign keys (ending in_id), it returns the name without the suffix (e.g.,plan_idreturnsplan).$history->oldValue(): Returns the value before the update. If the value is a foreign key, it usesidentifiableName()to return a meaningful string.$history->newValue(): Returns the value after the update. If the value is a foreign key, it usesidentifiableName()to return a meaningful string.
Listen to Revisionable events
masterRevisionable fires events whenever a revision is created, saved, or deleted. You can listen for these in your
EventServiceProviderusing therevisionable.*pattern.// app/Providers/EventServiceProvider.php public function boot() { parent::boot(); $events->listen('revisionable.*', function($model, $revisions) { // Do something with the revisions or the changed model. dd($model, $revisions); }); }