PHPackages                             fico7489/laravel-updated-related - 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. [Caching](/categories/caching)
4. /
5. fico7489/laravel-updated-related

ActiveLibrary[Caching](/categories/caching)

fico7489/laravel-updated-related
================================

Update Elasticsearch data or clear cache when the model or any related model is updated, created or deleted.

3.0.3(6y ago)19490MITPHP

Since Nov 14Pushed 6y ago1 watchersCompare

[ Source](https://github.com/fico7489/laravel-updated-related)[ Packagist](https://packagist.org/packages/fico7489/laravel-updated-related)[ Docs](https://github.com/fico7489/laravel-updated-related)[ RSS](/packages/fico7489-laravel-updated-related/feed)WikiDiscussions master Synced yesterday

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

Laravel Updated Related
=======================

[](#laravel-updated-related)

Update **Elasticsearch** data or **clear cache** when the model or any related model is updated, created or deleted.

Why to use
----------

[](#why-to-use)

In laravel, we have the ability to listen to changes on eloquent models(create, delete, update), and when the model is changed we can do something with model e.g. we can flush cache for this model.

But sometimes we want to do something with the model when this model is changed or related models are changed. For example, you have model Seller with related models Order, ZipCodes, Address and OrderItem. If any change on Seller, Address, Order, ZipCode or OrderItem is performed you want to do something with Seller e.g flush cache for seller because cache include related models data:

```
$seller = Cache::remember('seller_' . $id, $minutes, function () use($id){
    return Seller::with(['orders', 'orders.items', 'addresses', 'zipCodes'])->find($id);
});

```

In above case when Seller model is changed, we have to flush his cache 'seller'.$id but also when his particular related models are updated we have to flush data. Obviously we need some configuration with related models here and this package will help you with this.

You can add this functionality without the package, but there is another huge problem. You can map related models and parent model to know when to flush cache but if you have batch form, e.g. for update all ZipCodes, and if you have 1000 zip codes and hit save, cache will be flushed 1000 times. This is not a problem for a flushing cache but if you have to update Elasticsearch data or perform some other time-consuming operation then it is.

This package also solves above problem because model changed events are saved to an array and processed after the request when they are filtered to be unique, so if you update 1000 zip codes for the same seller only one event will be dispatched.

Version Compatibility
---------------------

[](#version-compatibility)

The package is available for larvel 5.\* versions.

Install
-------

[](#install)

1.Install package with composer

```
composer require fico7489/laravel-updated-related:"*"

```

2.Add service provider to config/app.php

```
Fico7489\Laravel\UpdatedRelated\Providers\UpdatedRelatedServiceProvider::class

```

3.Publish configuration

```
php artisan vendor:publish  --provider="Fico7489\Laravel\UpdatedRelated\Providers\UpdatedRelatedServiceProvider"

```

and after that adjust configuration(map model with related models), see more in below section.

4.Use this trait

```
Fico7489\Laravel\UpdatedRelated\Traits\UpdatedRelatedTrait

```

in your base model.

and that's it, you are ready to go.

Configuration
-------------

[](#configuration)

Configuration is located at config/laravel-updated-related.php

1.You can create configuration in simple way :

```
return [
    \App\Models\User::class => [
        [
            \App\Models\Address::class         => 'addresses',
            \App\Models\Order::class           => 'orders',
            \App\Models\OrderItem::class       => 'orders.items',
        ],
    ],
];

```

KEYS are base models, VALUES are arrays with related model =&gt; relation

2.Or you can create configuration in detailed way if you need more environments for the same base model. :

```
return [
    \App\Models\User::class => [
        [
            'name' => 'user-simple',
            'related' => [
                \App\Models\Address::class  => 'addresses',
            ],
        ],
        [
            'name' => 'user-extended',
            'related' => [
                \App\Models\Address::class   => 'addresses',
                \App\Models\OrderItem::class => 'orders.items',
            ],
        ],
    ]
];

```

KEYS are base models, VALUES are arrays with names (environment name) and related configuration (arrays with related model =&gt; relation). In "simple way" environment will be 'default'.

After you set a configuration just listened to the **ModelChanged** event that will be dispatched when any model or its related model (defined in configuration) is changed (updated, deleted, created).

One real example
----------------

[](#one-real-example)

Configuration :

```
return [
    \App\Models\User::class => [
        [
            \App\Models\Address::class => 'addresses',
        ],
    ],
];

```

Models :

```
...
class User extends BaseModel
{
    public function addresses()
    {
        return $this->hasMany(Address::class);
    }
...

```

```
...
class Address extends BaseModel
{
....

```

Event service provider :

```
...
protected $listen = [
    ModelChanged::class => [
        TestListener::class,
    ],
...

```

Listener :

```
use Fico7489\Laravel\UpdatedRelated\Events\ModelChanged;

class TestListener
{
    public function handle(ModelChanged $event)
    {
        echo 'id=' . $event->getId();
        echo 'model=' . $event->getModel();
        echo 'environment=' . $event->getName();
    }
}

```

```
User::find(1)->touch();
Address::find(2)->touch(); //let's assume that user_id is 10 here

```

With above code you will se this output:

```
id=1
model=App\Models\User
environment=default

id=10
model=App\Models\User
environment=default

```

If you create, delete or update User or Address model ModelChanged will be dispatched.

Cover all changes in the database
---------------------------------

[](#cover-all-changes-in-the-database)

Do not use code like this one:

```
User::whereIn('id', $ids)->update(['status' => 'ban']);

```

Because in that case, laravel does not use a model, it runs query directly without a model. To cover above changes you can change this code to :

```
$users = User::whereIn('id', $ids);
foreach($users as $user){
    $user->update(['status' => 'ban']);
}

```

### Pivot events

[](#pivot-events)

If you want to cover pivot events use this package :

When to use this package
------------------------

[](#when-to-use-this-package)

- clear cache for model
- update elasticsearch data
- and much more

License
-------

[](#license)

MIT

**Free Software, Hell Yeah!**

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity70

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 ~86 days

Recently: every ~215 days

Total

11

Last Release

2239d ago

Major Versions

1.3.x-dev → 2.0.02017-11-14

2.1.0 → 3.0.02017-12-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/949b29f724c44fb480deeab36c7c50624f1351bec04a5045546f6d9f7334dd27?d=identicon)[fico7489](/maintainers/fico7489)

---

Top Contributors

[![fico7489](https://avatars.githubusercontent.com/u/4559663?v=4)](https://github.com/fico7489 "fico7489 (57 commits)")

---

Tags

clear-cacheselasticsearchlaravel-elasticsearchlaravel-eventslaravellaravel-cachelaravel-elasticsearchlaravel related modelslaravel update relatedlaravel update events

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/fico7489-laravel-updated-related/health.svg)

```
[![Health](https://phpackages.com/badges/fico7489-laravel-updated-related/health.svg)](https://phpackages.com/packages/fico7489-laravel-updated-related)
```

###  Alternatives

[imanghafoori/laravel-widgetize

A minimal yet powerful package to give a better structure and caching opportunity for your Laravel apps.

909137.9k12](/packages/imanghafoori-laravel-widgetize)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

754.3k2](/packages/omaralalwi-lexi-translate)[suitmedia/laravel-cacheable

Decorate your repositories and make them cacheable

1237.7k](/packages/suitmedia-laravel-cacheable)[jeandormehl/laracache

InterSystems Caché provider for Laravel (ODBC)

143.6k](/packages/jeandormehl-laracache)[michele-angioni/support

Support is a Laravel package which promotes the use of best practices and design patterns.

181.4k1](/packages/michele-angioni-support)

PHPackages © 2026

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