PHPackages                             stayallive/laravel-eloquent-observable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. stayallive/laravel-eloquent-observable

ActiveLibrary[Database &amp; ORM](/categories/database)

stayallive/laravel-eloquent-observable
======================================

Register Eloquent model event listeners just-in-time directly from the model.

v1.1.0(2mo ago)2928.9k↓36.2%4MITPHPPHP ^8.0.2CI passing

Since Feb 28Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/stayallive/laravel-eloquent-observable)[ Packagist](https://packagist.org/packages/stayallive/laravel-eloquent-observable)[ Docs](https://github.com/stayallive/laravel-eloquent-observable)[ GitHub Sponsors](https://github.com/stayallive)[ RSS](/packages/stayallive-laravel-eloquent-observable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (8)Versions (7)Used By (4)

Laravel Eloquent Observable
===========================

[](#laravel-eloquent-observable)

[![Latest Version](https://camo.githubusercontent.com/7fe5843eb8260bfdc4d006253d07db96edcf20bd6d0c04863882e0d82ed890d5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73746179616c6c6976652f6c61726176656c2d656c6f7175656e742d6f627365727661626c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/stayallive/laravel-eloquent-observable/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/3131b65277aa9d3c1aab1088536959545971a094c4334cb159090623b7368720/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746179616c6c6976652f6c61726176656c2d656c6f7175656e742d6f627365727661626c652f63692e79616d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/stayallive/laravel-eloquent-observable/actions/workflows/ci.yaml)[![Total Downloads](https://camo.githubusercontent.com/30ee57680ba15a895cfb13191f1f5db2de4c080de6aeb3cfa41846c4a3f4fb3b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746179616c6c6976652f6c61726176656c2d656c6f7175656e742d6f627365727661626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/stayallive/laravel-eloquent-observable)

Register Eloquent model event listeners just-in-time directly from the model.

Using [Observers](https://laravel.com/docs/10.x/eloquent#observers) can introduce a (significant) overhead on the application since they are usually registered in a service provider which results in every model in your application with a observer is "booted" a startup of the application even though the model is never touched in the request. This package aims to reduce that overhead by connecting listeners just-in-time whenever the Eloquent model is booted (first used) in the request. The event callbacks are also defined on the model itself keeping the code cleaner, althought this is my preference of course and if you disagree this might not be the package for you.

Note

Laravel 10.44 [introduced](https://laravel-news.com/laravel-10-44-0#content-new-observedby-attribute-for-models) the `ObservedBy` attribute which allows you to define the observer on the model itself which negates all the performance benefits of this package. However this package still provides an alternative way to define the event handler methods on the model itself so might still be considered useful but using the `ObservedBy` attribute will solve the performance issue with registering the model observers inside a service provider.

Installation
------------

[](#installation)

```
composer require stayallive/laravel-eloquent-observable
```

Usage
-----

[](#usage)

Adding the `Observable` trait will ensure that the observable events are connected to the event handlers defined on the model.

```
