szana8/laraflow
LaraFlow is a Laravel package for adding workflows and state machines to Eloquent models. Define states, transitions, guards, and actions, then move records through controlled paths with events and validation for cleaner approval and status management.
Install the package and confirm Laravel/PHP compatibility before adopting it:
composer require szana8/laraflow
Check composer.json constraints and the GitHub issues because the last release is from April 2023.
Look first at:
php artisan vendor:publish --tag=laraflow-config or the tag documented by the package.Add the package trait to the Eloquent model that owns the workflow:
use Szana8\LaraFlow\Traits\HasFlow; // confirm exact namespace in README
class Order extends Model
{
use HasFlow;
protected $fillable = ['status'];
}
Ensure the model has a persisted state column, usually a string column:
Schema::table('orders', function (Blueprint $table) {
$table->string('status')->default('pending');
});
Scaffold a flow with the package generator, then define states and allowed transitions in the generated class. Exact generator names vary, so use the README/source:
php artisan make:flow OrderFlow
First use case: an order lifecycle.
// Illustrative only — confirm exact API in the package source.
$order->transitionTo('paid');
$order->canTransitionTo('shipped');
Start with one simple flow: pending -> paid -> shipped -> cancelled, add a guard for payment authorization, and listen to the package’s state-change event for audit logging.
DB::transaction(function () use ($order) {
$order->lockForUpdate()->find($order->id);
// domain work
$order->transitionTo('paid');
});
can/guard check before rendering buttons or accepting requests. Return 409 Conflict or 422 when a transition is not allowed.DB::afterCommit() for side effects so listeners do not run before the state is safely persisted.MarkAsPaid, ShipOrder, CancelOrder over generic UpdateStatus when the package supports transition classes.Order::where('status', 'pending') or a model scope if the package exposes state constants.pending_payment, not display labels.status column with unrelated machines.How can I help you explore Laravel packages today?