PHPackages                             igaster/laravel-model-events - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. igaster/laravel-model-events

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

igaster/laravel-model-events
============================

Laravel Custom Model Events

v2.0.2(5y ago)332621MITPHPPHP &gt;=7.1

Since Oct 28Pushed 5y ago2 watchersCompare

[ Source](https://github.com/igaster/laravel-model-events)[ Packagist](https://packagist.org/packages/igaster/laravel-model-events)[ Docs](https://github.com/igaster/laravel-model-events.git)[ RSS](/packages/igaster-laravel-model-events/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (8)Dependencies (3)Versions (9)Used By (0)

Description
-----------

[](#description)

[![Laravel](https://camo.githubusercontent.com/43b3e85f14e8fc6c53cd7c047358b29b574359480c62ded0137bd3f50ede699f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d6f72616e67652e737667)](http://laravel.com)[![License](https://camo.githubusercontent.com/4661abfe916186acde514558e7f040833cb63ba7098401a51ce339cbb2b4cf9e/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://tldrlegal.com/license/mit-license)[![Downloads](https://camo.githubusercontent.com/1de9bac8ff1a6b3da2b50b569f77232490bc81ce8d7ba05b4dd0b34b4a181cf8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696761737465722f6c61726176656c2d6d6f64656c2d6576656e74732e737667)](https://packagist.org/packages/igaster/laravel-model-events)[![Build Status](https://camo.githubusercontent.com/52d8249d0c79f9cb8206d90c60b356b349bfb287a68d84f764fe79ce3900e70a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f696761737465722f6c61726176656c2d6d6f64656c2d6576656e74732e737667)](https://travis-ci.org/igaster/laravel-model-events)[![Codecov](https://camo.githubusercontent.com/d2c280afdbbb50add7a63f78d33e49ca0afe714fd1e116d9f315688557d6344a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f696761737465722f6c61726176656c2d6d6f64656c2d6576656e74732e737667)](https://codecov.io/github/igaster/laravel-model-events)

This is as simple as keeping a diary for your models!

You can record a short message for any model with current timestamp and authenticated user.

Installation:
-------------

[](#installation)

A) Execute `composer require igaster/laravel-model-events`

This package includes a Service Provider that will be automatically discovered by Laravel.

B) Run migrations.

This will create a table `log_model_events` that will be used to store events.

Usage:
------

[](#usage)

### Step 1: Add a Trait to your model:

[](#step-1-add-a-trait-to-your-model)

```
use Igaster\ModelEvents\Traits\LogsModelEvents;

class MyModel extends Model
{
    use LogsModelEvents;
```

### Step 2: Log yout events:

[](#step-2-log-yout-events)

#### a) Manually

[](#a-manually)

Use the `logModelEvent("Description")` method to log any event

```
class MyModel extends Model
{
    public function myMethod()
    {
        // ...
        $modelEvent = $this->logModelEvent("Something Happened!");
    }
```

- The `logModelEvent()` method will also log a) the current authenticated user and b) the related model instance c) current timestamp
- This is a public method. You may also call it from your `$model` instance from anywhere

#### b) Automatically capture laravel model events:

[](#b-automatically-capture-laravel-model-events)

Eloquent models fire [several events ](https://laravel.com/docs/5.7/eloquent#events) during updating, creating etc. These events can be automatically logged. Just define these events inside the `$logModelEvents` static array in your model:

```
class MyModel extends Model
{
    public static $logModelEvents = [
        'created',
        'updated',
    ];
```

- Now every time this model instance is changed, the event will be logged and attributed to the authenticated user.
- As a bonus a report of all the updated attributes will be added in the description!

### Step 3: Fetch a list of events:

[](#step-3-fetch-a-list-of-events)

#### a) From a `$model` instance:

[](#a-from-a-model-instance)

```
// This will retrieve the last 10 events logged for $model instance.
$modelEvents = $model->getModelEvents(10);
```

#### b) From a `$user` instance:

[](#b-from-a-user-instance)

In order to query events from a $user model you must first include this trait with the User class: Note: This trait is optional for the rest functions of this package!

```
use Igaster\ModelEvents\Traits\UserLogsModelEvents;

class User extends Authenticatable
{
    use UserLogsModelEvents;
```

```
// This will retrieve the last 10 events logged by this $user.
$modelEvents = $user->getUserModelEvents(10);
```

#### c) Build your own queries:

[](#c-build-your-own-queries)

All relationships with the `LogModelEvent` model have been implemented. These are some valid queries:

```
$user->modelEvents; // Get all model events for $user
$model->modelEvents; // Get all model events for $model
$model->modelEvents()->where(`created_at`, '>', $yesterday)->get(); // Custom Query

// Or you can build queries with the LogModelEvent model:
LogModelEvent::whereUser($user)->whereModel($model)->get();
```

### Step 4: Display Events:

[](#step-4-display-events)

#### a) Manually

[](#a-manually-1)

Through a `LogModelEvents` model you can retrieve the `$user` and the `$model` instances:

```
foreach($model->modelEvents as $modelEvent){
    $modelEvent->user; // User model
    $modelEvent->model; // Model related with the event (though polymorphic relathinships)
    $modelEvent->description; // String
    $modelEvent->created_at;  // Timestamp
}
```

Note the the `$modelEvent->model` is a polymorphic relationship and it will retrieve a `$model` instance on its respective class.

#### b) Use package sample view:

[](#b-use-package-sample-view)

[![image](https://user-images.githubusercontent.com/4586319/47613088-cf211e00-da90-11e8-8e32-76e23976adc6.JPG)](https://user-images.githubusercontent.com/4586319/47613088-cf211e00-da90-11e8-8e32-76e23976adc6.JPG)

You may include the `model-events::modelEvents` partial in your views to render a list of events:

```

            Actions History:

            @include('model-events::modelEvents', [
                'model' => $order
            ])

```

Available parameters are: `model`, `user`, `count_events`. All are optional

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 94.1% 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 ~104 days

Recently: every ~93 days

Total

8

Last Release

2018d ago

Major Versions

v1.0.4 → v2.02020-10-29

PHP version history (2 changes)v1.0PHP &gt;=7.0.0

v1.0.1PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/55bf1c9bc1f0553b439f18852299833373f96eb6c1342434c74fb61c5aecd508?d=identicon)[igaster](/maintainers/igaster)

---

Top Contributors

[![igaster](https://avatars.githubusercontent.com/u/4586319?v=4)](https://github.com/igaster "igaster (32 commits)")[![koenhoeijmakers](https://avatars.githubusercontent.com/u/2232776?v=4)](https://github.com/koenhoeijmakers "koenhoeijmakers (2 commits)")

---

Tags

laravelpackage

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/igaster-laravel-model-events/health.svg)

```
[![Health](https://phpackages.com/badges/igaster-laravel-model-events/health.svg)](https://phpackages.com/packages/igaster-laravel-model-events)
```

###  Alternatives

[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)[okipa/laravel-table

Generate tables from Eloquent models.

56752.8k](/packages/okipa-laravel-table)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40129.3k2](/packages/erlandmuchasaj-laravel-gzip)[gallib/laravel-short-url

A Laravel package to shorten urls

16516.4k](/packages/gallib-laravel-short-url)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

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