PHPackages                             zidbih/laravel-auto-cache-clear - 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. zidbih/laravel-auto-cache-clear

ActiveLibrary[Caching](/categories/caching)

zidbih/laravel-auto-cache-clear
===============================

A Laravel package that automatically clears cache for Eloquent models using PHP attributes. Define cache-clearing behavior directly above your model with the #\[ClearCacheWhen\] attribute, and let the package handle the rest.

v1.1(1y ago)013MITPHPPHP ^8.1

Since Mar 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/medmahmoudhdaya/laravel-auto-cache-clear)[ Packagist](https://packagist.org/packages/zidbih/laravel-auto-cache-clear)[ RSS](/packages/zidbih-laravel-auto-cache-clear/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

Laravel Auto Cache Clear
========================

[](#laravel-auto-cache-clear)

A Laravel package that automatically clears cache for Eloquent models using PHP attributes. Define cache-clearing behavior directly above your model with the `#[ClearCacheWhen]` attribute, and let the package handle the rest.

---

Features
--------

[](#features)

- **Attribute-Based Configuration**: Use the `#[ClearCacheWhen]` attribute to define when cache should be cleared for a model.
- **Automatic Cache Clearing**: Automatically clears cache when specified model events occur.
- **Dynamic Cache Keys**: Define custom cache keys using placeholders like `{model}` and `{id}`.
- **Static Cache Keys**: Clear static cache keys (e.g., `"users"`) when model events occur.
- **Flexible and Extensible**: Works with any Eloquent model.

---

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

[](#installation)

### 1. Install the Package

[](#1-install-the-package)

```
composer require zidbih/laravel-auto-cache-clear
```

---

Usage
-----

[](#usage)

### 1. Add the `#[ClearCacheWhen]` Attribute to Your Model

[](#1-add-the-clearcachewhen-attribute-to-your-model)

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Zidbih\LaravelAutoCacheClear\Attributes\ClearCacheWhen;
use Zidbih\LaravelAutoCacheClear\Traits\AutoClearsCache;

#[ClearCacheWhen(['created', 'updated', 'deleted'], cacheKey: 'user:{id}:details', staticKeys: ['users'])]
class User extends Model
{
    use AutoClearsCache;
}
```

#### Attribute Parameters

[](#attribute-parameters)

- `events`: Model events that should trigger cache clearing (e.g., `['created', 'updated', 'deleted','restored']`).
- `cacheKey`: Cache key pattern (default: `{model}:{id}`). You can use placeholders like `{model}` and `{id}`.
- `staticKeys`: An array of static cache keys to clear (e.g., `['users','posts']`).

### 2. Use the `AutoClearsCache` Trait

[](#2-use-the-autoclearscache-trait)

```
use Zidbih\LaravelAutoCacheClear\Traits\AutoClearsCache;

class User extends Model
{
    use AutoClearsCache;
}
```

### 3. Cache Data in Your Application

[](#3-cache-data-in-your-application)

```
use Illuminate\Support\Facades\Cache;

// Cache a list of users
$users = Cache::remember('users', 60, function () {
    return User::all();
});

// Cache a single user
$user = Cache::remember('user:1:details', 60, function () {
    return User::find(1);
});
```

When a user is created, updated, or deleted, the cache keys `user:1:details` and `users` will automatically be cleared.

---

Example Workflow
----------------

[](#example-workflow)

### 1. Define the Model

[](#1-define-the-model)

```
#[ClearCacheWhen(['created', 'updated', 'deleted'], staticKeys: ['users'])]
class User extends Model
{
    use AutoClearsCache;
}
```

### 2. Cache Data

[](#2-cache-data)

```
$users = Cache::remember('users', 60*60*24, fn () => User::all());
```

### 3. Trigger Model Events

[](#3-trigger-model-events)

When a user is created, updated, or deleted, the following will be cleared:

- The static key: `users`

---

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to [open an issue](https://github.com/medmahmoudhdaya/laravel-auto-cache-clear/issues) or submit a pull request.

---

License
-------

[](#license)

This package is open-source software licensed under the [MIT license](LICENSE).

---

Credits
-------

[](#credits)

Developed by **Med Mahmoud Hadaya (zidbih)**
Inspired by the Laravel community.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance42

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

480d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b8435c1650e36d1af5d86c9955e343d4ce94ad62949a09e43c4f5cc1a47dba79?d=identicon)[medmahmoudhdaya](/maintainers/medmahmoudhdaya)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/zidbih-laravel-auto-cache-clear/health.svg)

```
[![Health](https://phpackages.com/badges/zidbih-laravel-auto-cache-clear/health.svg)](https://phpackages.com/packages/zidbih-laravel-auto-cache-clear)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k90.5k1](/packages/mike-bronner-laravel-model-caching)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[api-platform/laravel

API Platform support for Laravel

58171.4k14](/packages/api-platform-laravel)

PHPackages © 2026

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