nathandunn/laravel-state-history
Larablocks Analysis
nathandunn/laravel-state-history works with Laravel 12 and 13 (latest release v1.1.0, Nov 14, 2026).
Laravel 11 in earlier releases.
Recommendation checks
Solid choiceSolid choice: maintained and up to date, meets 2 of 4 checks; below the bar for Recommended on use.
- Works with Laravel 13, the current version
- Released in the last 12 months (Nov 14, 2026)
- 2,460 downloads a month (needs 10,000)
- 0 GitHub stars (needs 100)
State
Actively maintained- Last stable release: Nov 14, 2026.
- 4 releases in the last 12 months.
- First released: Aug 24, 2025.
- Monthly downloads: 2,460.
- 0 open issues on GitHub.
Data from Packagist and GitHub, checked by Larablocks on Nov 29, 2026.
| Install | |
|---|---|
composer require nathandunn/laravel-state-history |
|
| Latest Version: | v1.1.0 |
| PHP: | ^8.2 |
| Laravel: | 12 · 13 |
| Older releases: | 11 |
| License: | MIT |
| Last Updated: | Nov 14, 2026 |
| Links: | GitHub · Packagist |
🗃️ Laravel State History
A Laravel package for managing enum-based model states with enforced transitions and automatic history tracking.
Features
- Native PHP Enums (PHP 8.2+)
- Enforced Transitions with a pluggable state machine
- Automatic History with metadata
- Smart Casting of historical
from/tovalues (enums, dates, primitives, custom casts) - Atomic Operations – state change + history in one transaction
- Current State Columns (
current_{field}) for indexing & querying - Events, Guards & Effects for lifecycle hooks
- Laravel 12–13 Support
Installation
composer require nathandunn/laravel-state-history
php artisan vendor:publish --provider="NathanDunn\StateHistory\StateHistoryServiceProvider" --tag="migrations"
php artisan migrate
Quick Start
1. Define States
enum ArticleState: string
{
case Draft = 'draft';
case Published = 'published';
case Archived = 'archived';
}
2. Create a State Machine
use NathanDunn\StateHistory\Contracts\StateMachine;
use NathanDunn\StateHistory\TransitionMap;
class ArticleStateMachine implements StateMachine
{
public function getTransitions(): TransitionMap
{
return TransitionMap::build(ArticleState::class)
->allowFromNull(ArticleState::Draft)
->allow(ArticleState::Draft, ArticleState::Published)
->allow(ArticleState::Published, ArticleState::Archived)
->allowAnyTo(ArticleState::Draft);
}
}
3. Configure Your Model
use NathanDunn\StateHistory\Traits\HasState;
class Article extends Model
{
use HasState;
protected $casts = [
'state' => ArticleState::class,
];
protected function stateMachine(): array
{
return ['state' => ArticleStateMachine::class];
}
}
Usage
Transitions
$article = Article::create(['state' => ArticleState::Draft]);
$article->transitionTo('state', ArticleState::Published, meta: [
'editor' => 'alice'
]);
Querying
$published = Article::whereState('state', ArticleState::Published)->get();
if ($article->isInState('state', ArticleState::Published)) {
// published
}
$allowed = $article->getAllowedTransitions('state');
Current State
$state = $article->getState('state'); // ArticleState::Published
$raw = $article->getCurrentState('state'); // "published"
History
$history = $article->states('state');
foreach ($history as $h) {
$from = $h->from; // Enum instance
$to = $h->to;
$meta = $h->meta;
}
Advanced
Multiple State Fields
class Order extends Model
{
use HasState;
protected $casts = [
'status' => OrderStatus::class,
'payment_status' => PaymentStatus::class,
];
protected function stateMachine(): array
{
return [
'status' => OrderStateMachine::class,
'payment_status' => PaymentStateMachine::class,
];
}
}
Guards
use NathanDunn\StateHistory\Contracts\Guard;
class PublishedArticleGuard implements Guard
{
public function allows($model, $from, $to): bool
{
if ($to === ArticleState::Archived &&
$model->published_at < now()->subDays(30)) {
throw new \Exception('Must be published 30 days before archiving');
}
return true;
}
}
Effects
use NathanDunn\StateHistory\Contracts\Effect;
class PublishEffect implements Effect
{
public function execute($model, $from, $to): void
{
if ($to === ArticleState::Published) {
$model->update(['published_at' => now()]);
}
}
}
Events
StateTransitioning– fired before a transitionStateTransitioned– fired after success
use NathanDunn\StateHistory\Events\StateTransitioned;
Event::listen(StateTransitioned::class, function ($event) {
Log::info("Model {$event->model->id} {$event->from} → {$event->to}");
});
Current State Columns
Optional current_{field} columns improve indexing & analytics.
Schema::table('articles', function (Blueprint $t) {
$t->string('current_state')->nullable()->index();
});
Config (config/state-history.php):
return [
'use_current_columns' => true,
'prefix' => 'current_',
'model' => \App\Models\CustomStateHistory::class,
];
State Casting
History values auto-cast to configured types:
foreach ($article->states('state') as $h) {
$from = $h->from; // Enum
$to = $h->to;
}
Supports: enums, dates, primitives, custom casts.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.1.0 | ^8.2 | ^12.0| | MIT | |
| v1.0.6 | ^8.2 | ^11.0| | MIT | |
| v1.0.5 | ^8.2 | ^11.0| | MIT | |
| v1.0.4 | ^8.2 | ^11.0| | MIT | |
| v1.0.3 | ^8.2 | ^11.0| | MIT | |
| v1.0.2 | ^8.2 | ^11.0| | MIT | |
| v1.0.1 | ^8.2 | ^11.0| | MIT | |
| v1.0.0 | ^8.2 | ^11.0| | MIT |