SeviaFlow Get Started

Resource Events

Resource events are lifecycle hooks for work that SeviaFlow performs through its Resource operations. Use them for behavior that should accompany a create, update, delete, or restore operation, regardless of whether the write came from a form, API request, or supported import flow.

Use an event hook when the behavior belongs to the record lifecycle, such as setting an audit field. Use an Action for a named operation a person starts, or a Workflow transition for a change between business states. Direct Eloquent writes made outside SeviaFlow's Resource operations do not automatically run Resource event handlers; use Laravel model events if you need to observe those writes too.

Choose the event by timing

EventWhen it runsTypical use
creatingBefore a new record is saved.Set values such as created_by that should be included in the initial save.
createdAfter a new record has been created.Run follow-up logic that needs the saved record and its ID.
updatingBefore changes to an existing record are saved.Adjust or validate values as part of the current update.
updatedAfter an existing record has been updated.React to a completed update or inspect the changed values.
deleting, deletedBefore or after a record is deleted.Prepare for deletion or react after it succeeds.
restoring, restoredBefore or after a soft-deleted record is restored.Prepare or react to a restore operation.

Register a handler

Return a ResourceEvents object from the Resource's events() method. Each callback receives the affected model and a ResourceEventContext. In this example, the current operator's ID is assigned before a new customer is saved:

use App\Models\Customer;
use SeviaFlow\Core\Resources\ResourceEventContext;
use SeviaFlow\Core\Resources\ResourceEvents;

public static function events(ResourceEvents $events): ResourceEvents
{
    return $events->onCreating(
        function (Customer $record, ResourceEventContext $context): void {
            $record->created_by = $context->user()?->getAuthIdentifier();
        }
    );
}

You may register a closure, invokable class name, or an array of handlers. A class name is resolved from Laravel's container and the class must implement __invoke($record, $context). Register more than one handler for an event when separate pieces of application code need to react to it.

Read the event context

MethodWhat it tells you
user() / actor()The user associated with the operation, if there is one.
resourceClass(), resourceSlug()The Resource PHP class and the registered slug used by the application.
operation()The lifecycle stage, such as creating, updated, or deleted.
source()The SeviaFlow surface that initiated the operation, such as form, api, import, or relation.
original(), changes()Relevant values from before the operation and the values changed by it. Sensitive values are redacted.
request()The Laravel request when the operation has one; it may be null for system work.
metadata()Additional structured operation details. Sensitive values are redacted.
connection(), correlationId()The database connection name and an identifier useful for tracing related activity.

Resource handlers and Laravel events

For each supported lifecycle stage, SeviaFlow also dispatches a Laravel event such as SeviaFlow\Core\Events\Resources\ResourceCreated or ResourceUpdated. Use the Resource's events() method for behavior that belongs to that Resource. Use Laravel listeners when the application wants separate components to react without putting their code in the Resource class. Both receive the record and event context.