2.3 KiB
| title | description | extends | section |
|---|---|---|---|
| Hooks / The Event System | Hooks / The Event System | _layouts.documentation | content |
Hooks / The Event System
You can use event hooks to change the behavior of the package.
All hook callbacks receive the TenantManager as the first argument.
Tenant events
A common use case for these events is seeding the tenant data during creation:
// AppServiceProvider::boot()
tenancy()->hook('tenant.creating', function (TenantManager $tm, Tenant $tenant) {
$tenant->put([
'posts_per_page' => '15',
]);
});
The following events are available:
tenant.creatingtenant.createdtenant.updatingtenant.updatedtenant.deletingtenant.deletedtenant.softDeletingtenant.softDeleted
Callbacks for these events may accept the following arguments:
TenantManager $tenantManager, Tenant $tenant
Database events
A use case for these events is executing something after the tenant database is created (& migrated/seeded) without running into race conditions.
Say you have a AfterCreatingTenant job that creates a superadmin user. You may use the database.creating event to add this job into the queue chain of the job that creates the tenant's database.
tenancy()->hook('database.creating', function (TenantManager $tm, string $db, Tenant $tenant) {
return [
new AfterCreatingTenant($tenant->id);
]
});
The following events are available:
database.creatingdatabase.createddatabase.deletingdatabase.deleted
Callbacks for these events may accept the following arguments:
TenantManager $tenantManager, string $db, Tenant $tenant
Bootstrapping/ending events
The following events are available:
bootstrappingbootstrappedendingended
You may use the bootstrapping & ending events to prevent some bootstrappers from being executed.
The following actions can be prevented:
- database connection switch:
database - Redis prefix:
redis - CacheManager switch:
cache - Filesystem changes:
filesystem - Queue tenancy:
queue - and anything else listed in the [
tenancy.bootstrappersconfig]({{ $page->link('configuration#bootstrappers') }})
Callbacks for these events may accept the following arguments:
TenantManager $tenantManager, Tenant $tenant