- What is hyperf/model-listener used for?
- hyperf/model-listener adds Eloquent-style model event listeners to Hyperf database models, so you can hook into lifecycle events such as created, updated, and deleted. It helps keep business logic decoupled by moving side effects into dedicated listener classes instead of scattering them across services or controllers. It is aimed at Hyperf applications, not Laravel.
- How do I install hyperf/model-listener?
- Install it with Composer using composer require hyperf/model-listener. Then follow the package documentation to register the listener configuration in your Hyperf application. Because this package is lightweight, setup is mostly about defining listener classes and mapping them to the model events you care about.
- Does hyperf/model-listener work with Laravel?
- No. hyperf/model-listener is built for Hyperf database models and does not replace Laravel's Eloquent observers or event dispatcher. Laravel developers will recognize the listener style, but in a Laravel application you should use native model events and observers. Use this package only when your application is running on Hyperf.
- Which model events can I listen to with hyperf/model-listener?
- It supports model lifecycle hooks such as created, updated, and deleted, along with other events exposed by Hyperf database models. The exact event list depends on the model implementation and the package's listener mapping. Check the README for the supported event names before wiring production logic.
- How do I register a listener for a Hyperf model?
- Define a listener class for the lifecycle event you want to handle, then associate that listener with the target model through the package's registration mechanism. Keep each listener focused on one responsibility so the decoupling benefit is real. The README is the authoritative source for exact class names, configuration keys, and registration syntax.
- Is hyperf/model-listener a replacement for Laravel Eloquent observers?
- It is not a drop-in replacement for Laravel Eloquent observers because it targets Hyperf, not Laravel. It borrows the Eloquent-style idea of model-bound lifecycle listeners, which can make migration to Hyperf feel familiar. If you need Laravel observer behavior inside Laravel, use Laravel's built-in features instead.
- Can I use hyperf/model-listener in production?
- Yes, provided you treat listeners as production code: keep them small, avoid slow synchronous work in hot model paths, and offload heavy tasks to queues. Pay attention to transaction boundaries and exception handling, because listener failures can affect the model operation that triggered them. Add monitoring and tests for critical side effects before deploying.
- How do I test model listeners in Hyperf?
- Trigger the relevant model lifecycle event in a Hyperf test case and assert the listener's observable side effects. Isolate database state with transactions or refreshes, and mock external services so tests stay deterministic. Cover both successful listener execution and failure paths.
- Are there alternatives to hyperf/model-listener?
- Alternatives include using Hyperf's event dispatcher directly, explicit service-layer calls, or other model observer patterns available in your stack. The package is most useful when you want Eloquent-style model-bound listeners without a heavier abstraction. Evaluate it against your existing Hyperf conventions and team familiarity.
- Is hyperf/model-listener actively maintained?
- The provided repository metadata does not include release history or maintainer activity, so check Packagist and the source repository before adopting it. Confirm compatibility with your Hyperf version and review recent commits or issue responses. For long-lived applications, pin the version and read changelogs before upgrades.