Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Wallet Laravel Package

zotel/laravel-wallet

View on GitHub
Deep Wiki
Context7

Customizing events

Sometimes you want to modify the standard events of a package. This is done quite simply.

Let's add broadcast support? We need to implement our event from the interface.

use Zotel\Wallet\Internal\Events\BalanceUpdatedEventInterface;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

final class MyUpdatedEvent implements BalanceUpdatedEventInterface, ShouldBroadcast
{
    public function __construct(
        private \Zotel\Wallet\Models\Wallet $wallet,
        private DateTimeImmutable $updatedAt,
    ) {}
    
    public function getWalletId(): int { return $this->wallet->getKey(); }
    public function getWalletUuid(): string { return $this->wallet->uuid; }
    public function getBalance(): string { return $this->wallet->balanceInt; }
    public function getUpdatedAt(): DateTimeImmutable { return $this->updatedAt; }

    public function broadcastOn(): array
    {
        return $this->wallet->getAttributes();
    }
}

The event is ready, but that's not all. Now you need to implement your assembler class, which will create an event inside the package.

use Zotel\Wallet\Internal\Assembler\BalanceUpdatedEventAssemblerInterface;

class MyUpdatedEventAssembler implements BalanceUpdatedEventAssemblerInterface
{
    public function create(\Zotel\Wallet\Models\Wallet $wallet) : \Zotel\Wallet\Internal\Events\BalanceUpdatedEventInterface
    {
        return new MyUpdatedEvent($wallet, new DateTimeImmutable());
    }
}

Next, go to the package settings (wallet.php). We change the event to a new one.

    'assemblers' => [
        'balance_updated_event' => MyUpdatedEventAssembler::class,
    ],

Then everything is the same as with the standard events of the package.

use Zotel\Wallet\Internal\Events\BalanceUpdatedEventInterface;

protected $listen = [
    BalanceUpdatedEventInterface::class => [
        MyBalanceUpdatedListener::class,
    ],
];

And then we create a listener.

use Zotel\Wallet\Internal\Events\BalanceUpdatedEventInterface;

class MyBalanceUpdatedListener
{
    public function handle(BalanceUpdatedEventInterface $event): void
    {
        // And then the implementation...
    }
}

It's simple!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels