haztakki-yritys / recordings
Recordings gives your Eloquent models a complete history. Every edit becomes a new version, attributed to the user who made it, and nothing is lost until it leaves the Recycle Bin.
Requires
- php: ^8.5
- illuminate/console: ^13.0
- illuminate/database: ^13.0
- illuminate/support: ^13.0
- illuminate/validation: ^13.0
- red-explosion/laravel-sqids: ^2.1
Requires (Dev)
- larastan/larastan: ^3.15
- laravel/pint: ^1.32
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.3
- pestphp/pest-plugin-laravel: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-29 15:42:32 UTC
README
Introduction
Recordings gives your Eloquent models a complete history. Every edit becomes a new version, attributed to the user who made it, and nothing is lost until it leaves the Recycle Bin.
$recording = $recordings->record(new Post(['title' => 'Draft'])); $recordings->revise($recording, ['title' => 'Final']);
Installation
composer require haztakki-yritys/recordings php artisan vendor:publish --tag="haztakki-recordings-config" php artisan vendor:publish --tag="haztakki-recordings-migrations" php artisan migrate
Set the table names and tenancy in the config before you migrate. Your users, tenants and recordable models need integer keys.
Usage
Recordable models
Implement Versioned and use the Recordable trait. Your table needs no extra columns.
use Haztakki\Recordings\Concerns\Recordable; use Haztakki\Recordings\Contracts\Versioned; class Post extends Model implements Versioned { use Recordable; }
Recordable models only change through RecordingService, so saving or deleting one directly throws a LogicException.
$post->recording; $recording->recordableAs(Post::class);
Recording
$recording = $recordings->record(new Post(['title' => 'Draft'])); $reply = $recordings->record(new Comment(['body' => 'Looks good']), parent: $recording);
Revising
Changing a versioned column makes a new version. Nothing happens if nothing changed.
$recordings->revise($recording, ['title' => 'Final']);
Excluded columns update the current version instead of making a new one. Each change still appears in the history.
#[ExcludeFromHistory(['view_count'])] class Post extends Model implements Versioned
Rolling back
$recordings->rollBackTo($recording, $event);
History
$recording->events()->chronologically()->get(); $event->action; // RecordingAction::Updated $event->creator; // the user who made the change $event->payload; // what changed
Who made the change
Changes are credited to the signed-in user. In jobs and commands, say who is acting, or credit nobody.
$recordings->recordAs($user, fn () => $recordings->archive($recording)); $recordings->recordAsSystem(fn () => $recordings->archive($recording));
Changes with no creator throw, unless you turn off require_creator.
Add details to every event written inside a callback:
$recordings->withEventDetails(['source' => 'import'], fn () => ...);
Trees
$recordings->move($reply, $otherRecording); $recordings->move($reply, null); // to the top level
Ordering
$recordings->place([$first, $second], position: 0); $recordings->touch($recording->getKey(), Post::class);
touch marks a recording as recently active without writing history.
Archiving
Archiving takes a recording and everything under it out of view. Restoring brings them all back.
$recordings->archive($recording); $recordings->restore($recording);
Recycle Bin
The Recycle Bin lists each archived item once. Anything archived along with it comes back when it's restored. Register types in a service provider's boot method, so the purge command sees them too.
$recycleBin->register(Post::class, 'Post'); $recycleBin->archivedRootsVisibleTo($user)->paginate(); // what $user may see $recycleBin->restore($user, $identifiers); $recycleBin->deleteForever($user, $identifiers, reason: 'Duplicate');
Items deleted forever wait 90 days before they're purged, and can be recovered until then.
$recordings->recoverDiscarded($recording);
Archived items can also be deleted forever automatically.
'recycle_bin' => [ 'archived_retention_in_days' => 30, 'discarded_retention_in_days' => 90, ],
Implement DecidesRecycleBinVisibility to choose who sees which items, or DecidesRecycleBinRetention to set retention per tenant.
class OwnArchivedItems implements DecidesRecycleBinVisibility { public function limitToVisible(Builder $query, Model $user): void { $query->where('creator_id', $user->getKey()); } }
Purging
The purge removes rows, versions, history and stored files for good.
Schedule::command('recordings:purge')->daily();
php artisan recordings:purge --dry-run
Models that store files remove them once the purge commits.
class Upload extends Model implements Versioned, PurgesPhysicalStorage { use Recordable; public function purgePhysicalStorage(): void { Storage::delete($this->path); } }
Links
Links belong to the recording, so they survive every new version. Links to archived recordings are kept for when they're restored, and purging removes them. Define links in a service provider's boot method too.
$links->define('post.tags', new RecordingLink( from: Post::class, to: Tag::class, table: 'post_tag', fromColumn: 'post_id', toColumn: 'tag_id', )); $links->sync('post.tags', $recording, $tagKeys); $links->linked('post.tags', $recording)->get(); $links->linkedToEach('post.tags', $posts); $links->withCount('post.tags', $query, as: 'tags_count');
Querying
Queries only return active recordings.
Recordings::recordings()->ofType(Post::class)->get(); Recordings::recordings()->onlyArchived()->get(); Recordings::recordings()->onlyDiscarded()->get(); Recordings::recordings()->withInactive()->inTree($recording)->get();
$recording->isActive(); $recording->isArchived(); $recording->isDiscarded(); $recording->isA(Post::class); $recording->parent; $recording->children; $recording->root; $recording->creator;
Identifiers
Recordings use their id in route model binding, queued jobs and the Recycle Bin. You may switch to a uuid or a sqid instead.
'identifier' => 'id',
$recording->identifier(); Recording::keysFromIdentifiers($identifiers);
Tenancy
'tenant' => [ 'model' => Team::class, 'resolver' => CurrentTeam::class, ],
Every recording then belongs to one team. Queries, trees and links never cross into another.
Recordings::recordings()->withoutTenant()->get(); $tenancy->runAs($team->getKey(), fn () => ...);
Authorisation
You don't need a policy for recordings. Each check is passed to the policy of the model the recording holds.
RecordingService doesn't check permissions, so authorise before you call it. The Recycle Bin checks them itself.
Gate::allows('restore', $recording); // PostPolicy::restore()
Validation
'tags.*' => [RecordingRules::exists(Tag::class)], 'title' => [RecordingRules::unique(Post::class, 'title', ignore: $recording)],
exists() takes the configured identifier, so the input can be an id, uuid or sqid, whichever your app shows. A value that can't be an identifier fails validation.
Events
RecycleBinChanged is dispatched after each Recycle Bin change, and RecordingsPurging just before rows are purged.
Configuration
'models' => [ 'recording' => Recording::class, 'recording_event' => RecordingEvent::class, 'user' => null, ], 'tables' => [ 'recordings' => 'recordings', 'recording_events' => 'recording_events', ], 'require_creator' => true, 'transaction_attempts' => 3, 'policy' => RecordingPolicy::class,
Nothing is final, so any class can be extended.
Translations
php artisan vendor:publish --tag="haztakki-recordings-translations"
Testing
composer test
Licence
The MIT License (MIT). Please see the licence file for more information.