PHPackages                             avinash403/laravel-dynamic-observer - 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. avinash403/laravel-dynamic-observer

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

avinash403/laravel-dynamic-observer
===================================

package that allows observing eloquent models in model itself

v1.0.4(7y ago)3160[1 issues](https://github.com/avinash403/laravel-dynamic-observer/issues)OSL-1.0PHP

Since May 29Pushed 7y ago1 watchersCompare

[ Source](https://github.com/avinash403/laravel-dynamic-observer)[ Packagist](https://packagist.org/packages/avinash403/laravel-dynamic-observer)[ RSS](/packages/avinash403-laravel-dynamic-observer/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)Dependencies (4)Versions (4)Used By (0)

Laravel Dynamic Observer
========================

[](#laravel-dynamic-observer)

In laravel or any other framework which uses eloquent for data modelling, observer is a useful feature. But performing an action on model update without registering beforehand is handy.

Laravel Dynamic Observer provides an easy way to observe your model in model itself by simply importing it in the model and defining the listener methods.

Motivation
----------

[](#motivation)

It is real easy to create a trait yourself and use it with eloquent model events but it is not worth it in writing the same piece of code in all of your projects.Apart from this, there are cases where we need to perform same action on creation, updation and deletion of a model(for example, clearing a particular cache after a record is updated, created or deleted). In those scenarios we have an option of either writing the same piece of code in all of those event listener or make a common method and call it in all of them. The later scenario is the key motivation for making this package so that we can have a method in our model which gets called automatically as soon as any model activity(create, update or delete) happens.

Compatibility
-------------

[](#compatibility)

Any project with eloquent as database model.(can be a non-laravel project)

### Installation by Composer

[](#installation-by-composer)

```
$ composer require avinash403/laravel-dynamic-observer

```

### Usage example

[](#usage-example)

```
//import ObserveModel trait
use ObserveModel\ObserveModel

class ExampleModel extends Model
{
	//include trait in the test class
	use ObserveModel;

	/**
	 * will be called before the create operation happens on the current model
	 * @param  ExampleModel $model 		instance of ExampleModel
	 * @return void
	 */
	private function beforeCreate($model)
	{
		//write your logic here
	}

}
```

### Available methods

[](#available-methods)

- beforeCreate : gets called before a records gets created
- afterCreate : gets called after a record gets created
- beforeUpdate : gets called before a record gets updated
- afterUpdate : gets called after a record gets updated
- beforeDelete : gets called before a record is deleted
- afterDelete : gets called after a record is deleted
- beforeSave : gets called before save method is invoked
- afterSave : gets called after save method is invoked
- beforeModelActivity : gets called before any model activity
- afterModelActivity : gets called after any model activity
- afterRetrieve : gets called after a record is retrieved

### Point to remember

[](#point-to-remember)

***Methods related to update and delete only gets called if the record is retrieved from the DB before updating or deleting. The reason is when we are using an update directly on a query, we are not actually performing any action.***For eg.

```
Model::where('field','value')->update(['field'=>'new value']);

```

here we are calling `Eloquent\Builder@update` not `Model@update`, so it just cannot call update event. Same is applicable to delete event.

But when we are doing something like this:

```
Model::where('field','value')->first()->update(['field'=>'new value']);

```

We exactly know which record we have fetched. In this case we have used `Model@update`, which will cause event to fire.

### Test

[](#test)

Run test by simply typing `composer test`from the root directory of this package.

### Contribute on github

[](#contribute-on-github)

clone this repository(  ), make your changes and raise a pull request to development branch

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance13

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

3

Last Release

2901d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9025cebb7b1dee4016c166d70d8171c7a36f9908dbcfa6bfbb341950b88e8ec4?d=identicon)[avinash403](/maintainers/avinash403)

---

Top Contributors

[![avinash403](https://avatars.githubusercontent.com/u/31030044?v=4)](https://github.com/avinash403 "avinash403 (21 commits)")

---

Tags

laraveleloquentobserverdynamic observer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/avinash403-laravel-dynamic-observer/health.svg)

```
[![Health](https://phpackages.com/badges/avinash403-laravel-dynamic-observer/health.svg)](https://phpackages.com/packages/avinash403-laravel-dynamic-observer)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[betapeak/laravel-auditing-filesystem

A filesystem driver for the owen-it/laravel-auditing package. Allows storage of the audits in CSV files, across all registered Laravel disks.

166.5k](/packages/betapeak-laravel-auditing-filesystem)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
