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
| Event | Fired When | Cancellable |
|---|---|---|
Creating | Before a new record is created | Yes |
Created | After a new record is created | No |
Updating | Before an existing record is updated | Yes |
Updated | After an existing record is updated | No |
Saving | Before any create or update | Yes |
Saved | After any create or update | No |
Deleting | Before a record is deleted | Yes |
Deleted | After a record is deleted | No |
Attaching | Before an association is created | Yes |
Attached | After an association is created | No |
Detaching | Before an association is removed | Yes |
Detached | After an association is removed | No |
StageChanging | Before a pipeline stage transition | Yes |
StageChanged | After a pipeline stage transition | No |
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
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:
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.
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.
| Event | Fired When | Cancellable |
|---|---|---|
BatchCreating | Before createMany() executes | Yes |
BatchCreated | After createMany() completes | No |
BatchUpdating | Before updateMany() executes | Yes |
BatchUpdated | After updateMany() completes | No |
BatchUpserting | Before upsertMany() executes | Yes |
BatchUpserted | After upsertMany() completes | No |
BatchDeleting | Before deleteMany() executes | Yes |
BatchDeleted | After deleteMany() completes | No |
All batch events extend Rollogi\LaravelHubspot\Events\BatchModelEvent and carry two properties:
$event->records-- aCollectionof the records being processed$event->objectType-- the HubSpot object type string (e.g.contacts)
Listening for Batch Events
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:
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:
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.