Skip to content

Events

The package dispatches Laravel events during CRUD operations and association changes, following Eloquent's before/after convention. "Before" events (e.g. Creating, Saving) are cancellable -- return false from a listener to abort the operation.

Available Events

EventFired WhenCancellable
CreatingBefore a new record is createdYes
CreatedAfter a new record is createdNo
UpdatingBefore an existing record is updatedYes
UpdatedAfter an existing record is updatedNo
SavingBefore any create or updateYes
SavedAfter any create or updateNo
DeletingBefore a record is deletedYes
DeletedAfter a record is deletedNo
AttachingBefore an association is createdYes
AttachedAfter an association is createdNo
DetachingBefore an association is removedYes
DetachedAfter an association is removedNo
StageChangingBefore a pipeline stage transitionYes
StageChangedAfter a pipeline stage transitionNo

All events live under the Rollogi\LaravelHubspot\Events namespace.

Pipeline Stage Events

StageChanging and StageChanged are fired during pipeline stage transitions on Deals and Tickets. For more details on pipeline management, see the Pipelines page.

Listening for Events

php
use Illuminate\Support\Facades\Event;
use Rollogi\LaravelHubspot\Events\Created;

Event::listen(Created::class, function (Created $event): void {
    $model = $event->model;
    logger()->info(sprintf('HubSpot %s created: %s', $model->type(), $model->id));
});

Cancelling Operations

Return false from a listener to prevent the operation:

php
use Illuminate\Support\Facades\Event;
use Rollogi\LaravelHubspot\Events\Deleting;

Event::listen(Deleting::class, function (Deleting $event): bool {
    // Prevent deletion of contacts with active deals
    return $event->model->type() !== 'contacts';
});

Association Events

Association events (Attaching, Attached, Detaching, Detached) provide the source model, target model, and target ID. They fire for individual IDs during attach(), detach(), sync(), syncWithoutDetaching(), and toggle() operations.

php
use Illuminate\Support\Facades\Event;
use Rollogi\LaravelHubspot\Events\Attaching;
use Rollogi\LaravelHubspot\Events\Detaching;

Event::listen(Attaching::class, function (Attaching $event): void {
    logger()->info(sprintf(
        'Associating %s(%s) -> %s(%s)',
        $event->source->type(),
        $event->source->id,
        $event->target->type(),
        $event->targetId,
    ));
});

// Cancel detaching a specific ID
Event::listen(Detaching::class, function (Detaching $event): ?false {
    if ($event->targetId === 'protected-id') {
        return false; // Prevents this ID from being detached
    }
    return null;
});

When a before-event (Attaching or Detaching) returns false for a specific ID during a sync operation, only that ID is skipped -- other IDs in the batch proceed normally.