PHPackages                             foxws/laravel-modelcache - 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. foxws/laravel-modelcache

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

foxws/laravel-modelcache
========================

Cache helpers for Laravel Eloquent models

1.2.0(2mo ago)24.2k[1 PRs](https://github.com/foxws/laravel-modelcache/pulls)MITPHPPHP ^8.2CI passing

Since Oct 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/foxws/laravel-modelcache)[ Packagist](https://packagist.org/packages/foxws/laravel-modelcache)[ Docs](https://github.com/foxws/laravel-modelcache)[ GitHub Sponsors](https://github.com/Foxws)[ RSS](/packages/foxws-laravel-modelcache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (30)Versions (12)Used By (0)

Laravel model cache helper
==========================

[](#laravel-model-cache-helper)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9aab1b649871aa61830cbf64c38c8032c829ebb0defdc3fe0f90b23277175cb7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f7877732f6c61726176656c2d6d6f64656c63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxws/laravel-modelcache)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0babfdbac8c1c5f4c856736c245639951a336a786c02061eb83aa5153235437e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7877732f6c61726176656c2d6d6f64656c63616368652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/foxws/laravel-modelcache/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/023350db213ec95fd735d90e4e0a62b08b439cf89d14d0527182b231953807e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7877732f6c61726176656c2d6d6f64656c63616368652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/foxws/laravel-modelcache/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b2ae0f6da0aedca6ec9c81c34f43d089fbf208577478d0acba28591ecb1ac0bc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f7877732f6c61726176656c2d6d6f64656c63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxws/laravel-modelcache)

This package allows the Laravel Cache driver to be easily used for model instances. By default, logged in users will have their own separate cache-prefix.

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

[](#installation)

Install the package via composer:

```
composer require foxws/laravel-modelcache
```

Publish the config file with:

```
php artisan vendor:publish --tag="modelcache-config"
```

Usage
-----

[](#usage)

### Model Concern

[](#model-concern)

Implement the `Foxws\ModelCache\Concerns\InteractsWithModelCache` trait to your Eloquent model:

```
use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    use InteractsWithModelCache;
}
```

### Facade

[](#facade)

It is also possible to use the `ModelCache` Facade directly:

```
use Foxws\ModelCache\Facades\ModelCache;

class MyActionClass
{
    public function handle(Video $model): void
    {
        if (! ModelCache::enabled()) {
            // modelcaching is disabled
            return;
        }

        ModelCache::cache($model, 'foo', 'bar');
        ModelCache::hasBeenCached($model, 'foo');
        ModelCache::getCachedValue($model, 'foo');
    }
}
```

### Model instance

[](#model-instance)

To put a cache value for a model instance:

```
Video::first()->modelCache('currentTime', 20);
Video::first()->modelCache('randomSeed', 20, now()->addDay()); // cache for one day
```

To retrieve a cached model instance value:

```
Video::first()->modelCached('currentTime');
Video::first()->modelCached('randomSeed', $default); // with fallback
```

To validate if a cached model instance value exists:

```
$model = Video::findOrFail(10);

if (! $model->hasModelCache('currentTime')) {
    $model->modelCache('currentTime', 20);
}

return $model->modelCached('currentTime');
```

To forget a cached model value:

```
Video::first()->modelCacheForget('currentTime');
Video::first()->modelCacheForget('randomSeed');
```

### Model caching (global)

[](#model-caching-global)

To put a model cache value based on its class:

```
Video::setModelCache('randomSeed', 0.1);
Video::setModelCache('randomSeed', 0.1, now()->addDay()); // cache for one day
```

To retrieve a model class cached value:

```
Video::getModelCache('randomSeed');
Video::getModelCache('randomSeed', $default);
```

To validate if a model class cached value exists:

```
Video::hasModelCache('randomSeed');
```

To forget a model class cached value:

```
Video::forgetModelCache('randomSeed');
```

### Creating a custom cache profile

[](#creating-a-custom-cache-profile)

To determine which values should be cached, a cache profile class is used. The default class that handles these questions is `Foxws\ModelCache\CacheProfiles\CacheAllSuccessful`.

You can create your own cache profile class by implementing the `Foxws\ModelCache\CacheProfile\CacheProfile`, and overruling the `cache_profile` in `config/modelcache.php`.

It is also possible to overrule the cache prefix using the model instance. For this create a method named `cacheNameSuffix` on the model instance:

```
use Foxws\ModelCache\Concerns\InteractsWithModelCache;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Video extends Model
{
    use InteractsWithModelCache;

    protected function cacheNameSuffix(string $key): string
    {
        // return Auth::check()
        //     ? (string) Auth::id()
        //     : '';

        // return "{$key}:{$this->getMorphClass()}";

        return ''; // do not use a separate cache for users
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

This package is entirely based on the [space/laravel-responsecache](https://github.com/spatie/laravel-responsecache/) package.

Please consider to sponsor Spatie, such as purchasing their excellent courses. :)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance89

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.9% 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 ~72 days

Recently: every ~126 days

Total

8

Last Release

62d ago

Major Versions

0.0.4 → 1.0.02024-12-27

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5028905?v=4)[François M.](/maintainers/francoism90)[@francoism90](https://github.com/francoism90)

---

Top Contributors

[![francoism90](https://avatars.githubusercontent.com/u/5028905?v=4)](https://github.com/francoism90 "francoism90 (40 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")

---

Tags

cachecachingeloquentfoxwshelperlaravelmodelsredislaravelmodeleloquentcachecachingmodelslaravel-modelcache

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/foxws-laravel-modelcache/health.svg)

```
[![Health](https://phpackages.com/badges/foxws-laravel-modelcache/health.svg)](https://phpackages.com/packages/foxws-laravel-modelcache)
```

###  Alternatives

[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[ymigval/laravel-model-cache

Laravel package for caching Eloquent model queries

7642.2k3](/packages/ymigval-laravel-model-cache)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17649.9k](/packages/lacodix-laravel-model-filter)

PHPackages © 2026

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