Four events live in RoundlyConsulting\Addresses\Events. Each carries one public property, $address, uses Dispatchable and SerializesModels, and implements ShouldDispatchAfterCommit:
| 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 — and restore() when a deleted primary comes back as the primary. 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
}
}After commit
Outside a transaction an event dispatches as soon as the write is done. Inside a host transaction it waits for the commit, in the order the writes happened, and a rollback discards it — so a listener never reacts to an address that was never stored, or to an update or delete that was undone:
use Illuminate\Support\Facades\DB;
use RoundlyConsulting\Addresses\Facades\Addresses;
DB::transaction(function () use ($customer, $data, $order): void {
Addresses::for($customer)->add($data); // AddressCreated is held back…
$order->save();
}); // …and dispatched here, once committedEvent::fake() records a dispatch immediately, so it does not show this deferral; assert it with a real Event::listen listener and a transaction.
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.
- A restore that brings the address back plain, because another primary took the slot meanwhile, fires no PrimaryAddressChanged, and restoreQuietly() fires nothing.
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.