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.

Batch Lifecycle Events

Batch operations (createMany, updateMany, upsertMany, deleteMany) fire their own lifecycle events. These are separate from the per-model events listed above.

EventFired WhenCancellable
BatchCreatingBefore createMany() executesYes
BatchCreatedAfter createMany() completesNo
BatchUpdatingBefore updateMany() executesYes
BatchUpdatedAfter updateMany() completesNo
BatchUpsertingBefore upsertMany() executesYes
BatchUpsertedAfter upsertMany() completesNo
BatchDeletingBefore deleteMany() executesYes
BatchDeletedAfter deleteMany() completesNo

All batch events extend Rollogi\LaravelHubspot\Events\BatchModelEvent and carry two properties:

  • $event->records -- a Collection of the records being processed
  • $event->objectType -- the HubSpot object type string (e.g. contacts)

Listening for Batch Events

php
use Illuminate\Support\Facades\Event;
use Rollogi\LaravelHubspot\Events\BatchCreating;
use Rollogi\LaravelHubspot\Events\BatchCreated;

Event::listen(BatchCreating::class, function (BatchCreating $event): void {
    logger()->info(sprintf('Creating %d %s', $event->records->count(), $event->objectType));
});

Event::listen(BatchCreated::class, function (BatchCreated $event): void {
    logger()->info(sprintf('Created %d %s', $event->records->count(), $event->objectType));
});

Cancelling a Batch

Return false from a before-event listener to cancel the entire batch operation:

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

Event::listen(BatchCreating::class, function (BatchCreating $event): ?false {
    if ($event->objectType === 'contacts' && $event->records->count() > 500) {
        return false; // Cancels the entire batch
    }

    return null;
});

Filtering Individual Records

Before-event listeners can mutate $event->records to filter out specific items before the batch executes:

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

Event::listen(BatchCreating::class, function (BatchCreating $event): void {
    $event->records = $event->records->filter(
        fn (array $record): bool => !empty($record['email']),
    );
});

If the filtered collection is empty, the batch operation is skipped entirely.