Four events live in RoundlyConsulting\Addresses\Events. Each carries one public property, $address, and uses Dispatchable and SerializesModels:
| Event | Fired when |
|---|---|
AddressCreated | An address is created through the package — Addresses::for()->add(), the builder’s save(), addAddress(), createAddress() or CreateAddressAction. |
AddressUpdated | Addresses::update() — UpdateAddressAction — updates an address. |
AddressDeleted | An Address model instance is deleted — soft delete or forceDelete(). |
PrimaryAddressChanged | An address becomes the primary of its type — setPrimary() or setPrimaryAddress(), adding one with isPrimary: true or ->primary(), or updating one with isPrimary: true. Fires after AddressCreated / AddressUpdated, once the promotion is written; not when the address already was the primary. |
use Illuminate\Support\Facades\Event;
use RoundlyConsulting\Addresses\Events\AddressCreated;
use RoundlyConsulting\Addresses\Events\PrimaryAddressChanged;
// e.g. in AppServiceProvider::boot()
Event::listen(function (AddressCreated $event): void {
$address = $event->address; // the new Address
$owner = $event->address->addressable; // its owner
});
Event::listen(function (PrimaryAddressChanged $event): void {
// e.g. recalculate shipping for $event->address->addressable
});Or use a dedicated listener class:
namespace App\Listeners;
use RoundlyConsulting\Addresses\Events\AddressUpdated;
final class RecalculateTaxOnAddressChange
{
public function handle(AddressUpdated $event): void
{
$owner = $event->address->addressable;
// recalculate tax for $owner
}
}What does not fire events
- Raw Eloquent writes — $customer->addresses()->create([...]), $address->update([...]) — and model factories fire no AddressCreated or AddressUpdated.
- Bulk query deletes such as $customer->addresses()->delete() fire no AddressDeleted.
- Promoting or re-saving an address that already is the primary fires no PrimaryAddressChanged — see Primary addresses.
Because the events use SerializesModels, queued listeners receive the Address re-fetched from the database.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.