PHPackages                             rogervila/laravel-autocache - 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. rogervila/laravel-autocache

ActiveLibrary[Caching](/categories/caching)

rogervila/laravel-autocache
===========================

Automatic Laravel Eloquent Models Cache

0.5.0(5y ago)544[1 issues](https://github.com/rogervila/laravel-autocache/issues)[3 PRs](https://github.com/rogervila/laravel-autocache/pulls)MITPHP

Since Aug 7Pushed 4y ago1 watchersCompare

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

READMEChangelog (7)Dependencies (7)Versions (12)Used By (0)

[![Laravel Autocache](https://camo.githubusercontent.com/8e12a0018efe15f9e7ef071c56b4d37692619f568c8cdd617c4e99520cac03c1/68747470733a2f2f696d6167652e666c617469636f6e2e636f6d2f69636f6e732f7376672f38332f38333838302e737667)](https://camo.githubusercontent.com/8e12a0018efe15f9e7ef071c56b4d37692619f568c8cdd617c4e99520cac03c1/68747470733a2f2f696d6167652e666c617469636f6e2e636f6d2f69636f6e732f7376672f38332f38333838302e737667)

[![Build Status](https://camo.githubusercontent.com/b143abea2bfeac9045f32389c449b92df6a4afd3db19aecaa84a35733cfd9a66/68747470733a2f2f7472617669732d63692e6f72672f726f67657276696c612f6c61726176656c2d6175746f63616368652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/rogervila/laravel-autocache)[![Build status](https://camo.githubusercontent.com/68a5352e6b0cf5f45358731a49d890ee5a1a8a9432951532ebaadf1a0ef076e0/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f336e616a653539346a387270763378392f6272616e63682f6d61737465723f7376673d74727565)](https://ci.appveyor.com/project/roger-vila/laravel-autocache/branch/master)[![StyleCI](https://camo.githubusercontent.com/368485e85a0e0882bdcae697a38044e6b246d5d1b6256c7c320040a6d79ea02e/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3137393133323637362f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/179132676)[![Quality Gate Status](https://camo.githubusercontent.com/7881c4922b26202d79aba645731ea073cbd76a31fbd267d7107bb2d6274600ee/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d726f67657276696c615f6c61726176656c2d6175746f6361636865266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=rogervila_laravel-autocache)[![Coverage](https://camo.githubusercontent.com/e12bedf19cbab55b1f93052c7ce057cda255c2ffccd2b60c10694a5e1d5beeb8/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d726f67657276696c615f6c61726176656c2d6175746f6361636865266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=rogervila_laravel-autocache)[![Maintainability Rating](https://camo.githubusercontent.com/d4679d3916b48ff8c31adec510acb47b52d46866539110c6c6ef3c8cac3384d8/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d726f67657276696c615f6c61726176656c2d6175746f6361636865266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=rogervila_laravel-autocache)[![Latest Stable Version](https://camo.githubusercontent.com/1d699ebe34fdb7124be458825dc805715f064e44bd0526353281e30f6e9cdc16/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f6c61726176656c2d6175746f63616368652f762f737461626c65)](https://packagist.org/packages/rogervila/laravel-autocache)[![Total Downloads](https://camo.githubusercontent.com/22983fc2f105c01b65c69466fd8a61977bac1ce05318f9874759442021c06c93/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f6c61726176656c2d6175746f63616368652f646f776e6c6f616473)](https://packagist.org/packages/rogervila/laravel-autocache)[![License](https://camo.githubusercontent.com/a14766ed99f171a859193978032b4083becf975c5776af85dd2b0d1ad902e62d/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f6c61726176656c2d6175746f63616368652f6c6963656e7365)](https://packagist.org/packages/rogervila/laravel-autocache)

Laravel Autocache
=================

[](#laravel-autocache)

About
-----

[](#about)

Laravel Autocache package caches Eloquent model 'select' queries.

When a model is modified with [Eloquent methods](https://laravel.com/docs/5.8/eloquent#inserting-and-updating-models), the cache is automatically flushed.

Example
-------

[](#example)

Imagine that we select all Posts with Categories using [Eager Loading](https://laravel.com/docs/5.8/eloquent-relationships#eager-loading)

```
Post::with('categories')->get();
```

This will generate two queries that will run only once:

```
select * from `posts`
select * from `categories`
```

While no Post or Category model change is applied, both queries will be cached.

Now, imagine that we edit the title of our latest post

```
$post = Post::find($id);
$post->update(['title' => 'Your edited title']);

return Post::with('categories')->get();
```

Only the select query for `posts` table will be executed, since Category model stills cached

```
select * from `posts`
```

Autocache can be disabled on runtime if necessary

```
Post::disableAutoCache();

// Do your database changes here

Post::enableAutoCache();
```

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

[](#installation)

Require this package with composer.

```
composer require rogervila/laravel-autocache
```

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

```
LaravelAutoCache\AutocacheServiceProvider::class,
```

Now, copy the package config to your project config with the publish command:

```
php artisan vendor:publish --provider=" LaravelAutoCache\AutocacheServiceProvider"
```

Usage
-----

[](#usage)

First, put the models you want to handle into `config/autocache.php` models key.

```
/**
 * List of models that implement autocache by default.
 * Models have to also implement the Autocache trait
 * in order to work properly
 */
'models' => [
    App\Product::class,
],
```

Then, add the `Autocache` trait on the models listed on the configuration

```
namespace App;

use Illuminate\Database\Eloquent\Model;
use LaravelAutoCache\Autocache;

class Product extends Model
{
    use Autocache;
    ...
```

Troubleshooting
---------------

[](#troubleshooting)

#### Autocache does not work on update queries

[](#autocache-does-not-work-on-update-queries)

Check this [Laravel issue comment](https://github.com/laravel/framework/issues/11777#issuecomment-170384117)

License
-------

[](#license)

Laravel Autocache is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

Icon made by [Dirtyworks](https://www.flaticon.com/authors/dirtyworks "Dirtyworks") from [www.flaticon.com](https://www.flaticon.com/ "Flaticon") is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/ "Creative Commons BY 3.0")

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.2% 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 ~70 days

Recently: every ~106 days

Total

7

Last Release

2045d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/351443b7d23e94fcf31b250db90f0b9578cc9fd8e0cefbed9666467e3e9cb571?d=identicon)[rogervila](/maintainers/rogervila)

---

Top Contributors

[![rogervila](https://avatars.githubusercontent.com/u/6053012?v=4)](https://github.com/rogervila "rogervila (13 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (5 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rogervila-laravel-autocache/health.svg)

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

###  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)[swayok/alternative-laravel-cache

Replacements for Laravel's redis and file cache stores that properly implement tagging idea. Powered by cache pool implementations provided by http://www.php-cache.com/

202541.1k6](/packages/swayok-alternative-laravel-cache)[alexmg86/laravel-sub-query

Laravel subquery

7538.4k](/packages/alexmg86-laravel-sub-query)[laravel-enso/rememberable

Model caching dependency for Laravel Enso

2863.2k25](/packages/laravel-enso-rememberable)[byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.

1413.9k](/packages/byerikas-cache-tags)

PHPackages © 2026

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