PHPackages                             slydeath/laravel-nested-caching - 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. slydeath/laravel-nested-caching

ActiveLibrary[Caching](/categories/caching)

slydeath/laravel-nested-caching
===============================

Nested Caching for Laravel with caches stack

3.0(2y ago)111.9k1MITPHPPHP ^7.4|^8.1

Since Jul 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/SlyDeath/laravel-nested-caching)[ Packagist](https://packagist.org/packages/slydeath/laravel-nested-caching)[ RSS](/packages/slydeath-laravel-nested-caching/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (1)Versions (7)Used By (0)

Nested Caching for Laravel with caches stack
============================================

[](#nested-caching-for-laravel-with-caches-stack)

[![Latest Stable Version](https://camo.githubusercontent.com/a9b0cf4dc9022ca163894939fba10d0e87241a78866f1f95ba66ca56e6a0b39d/68747470733a2f2f706f7365722e707567782e6f72672f736c7964656174682f6c61726176656c2d6e65737465642d63616368696e672f762f737461626c65)](https://packagist.org/packages/slydeath/laravel-nested-caching)[![Total Downloads](https://camo.githubusercontent.com/5bd665dd6372faae04d8fddc4b9edec6557a2e2a32031bce7f08ecef7c967466/68747470733a2f2f706f7365722e707567782e6f72672f736c7964656174682f6c61726176656c2d6e65737465642d63616368696e672f646f776e6c6f616473)](https://packagist.org/packages/slydeath/laravel-nested-caching)[![License](https://camo.githubusercontent.com/c312936c27b321e62e6ba6a44cecbef9119a403e9900dd8c0cfd2cf1130f4b6d/68747470733a2f2f706f7365722e707567782e6f72672f736c7964656174682f6c61726176656c2d6e65737465642d63616368696e672f6c6963656e7365)](https://packagist.org/packages/slydeath/laravel-nested-caching)

Minimum requirements
--------------------

[](#minimum-requirements)

- PHP 7.4
- Laravel 8

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

[](#installation)

Add package to composer.json:

```
composer require slydeath/laravel-nested-caching
```

Open `config/app.php` and add the service provider to the array `providers`:

```
SlyDeath\NestedCaching\NestedCachingServiceProvider::class,
```

To place the configuration file, run:

```
php artisan vendor:publish --provider="SlyDeath\NestedCaching\NestedCachingServiceProvider" --tag=config
```

How to use?
-----------

[](#how-to-use)

### Caching any HTML chunk

[](#caching-any-html-chunk)

To cache any HTML chunk, you just need to pass the caching key to the `@cache` directive fragment:

```
@cache('simple-cache')

    This is an arbitrary piece of HTML that will be cached
    using the «simple-cache» key

@endCache
```

### Model caching

[](#model-caching)

To enable model caching support, add the trait to it `NestedCacheable`:

```
use SlyDeath\NestedCaching\NestedCacheable;

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

In the template, to cache a model, you need to pass its instance to the `@cache` directive:

```
@cache($user)
App\User model caching:

    Name: {{ $user->name }}
    Email: {{ $user->email }}

@endCache
```

### Caching the model for a specified time

[](#caching-the-model-for-a-specified-time)

To cache the model for a certain time, specify the lifetime in minutes as the second parameter:

```
@cache($user, 1440)
...
@endCache
```

#### Updating the «parent»

[](#updating-the-parent)

For update the cache of the «parent model», we need setup touches:

```
use SlyDeath\NestedCaching\NestedCacheable;

class CarUser extends Model
{
    use NestedCacheable;

    // Specifying the parent relations
    protected $touches = ['user'];

    // Parent relation
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
```

Usage example:

**resources/views/user.blade.php**

```
@cache($user)

    User's cars {{ $user->name }}

        @foreach($user->cars as $car)
            @include('user-car');
        @endforeach

@endCache
```

**resources/views/user-car.blade.php**

```
@cache($car)
{{ $car->brand }}
@endCache
```

### Collection caching

[](#collection-caching)

Example of caching a collection:

```
@cache($users)
@foreach ($users as $user)
    @include('user');
@endforeach
@endCache
```

### How to remove stack cache?

[](#how-to-remove-stack-cache)

Just run this code at bottom of your page:

```
app(SlyDeath\NestedCaching\CacheStack::class)->clearCache();
```

Workflow with another caches
----------------------------

[](#workflow-with-another-caches)

### How to collect keys?

[](#how-to-collect-keys)

Keys are automatically collected in `SlyDeath\NestedCaching\CacheWrittenListener` if `another-caching` is enabled, but you must save them manually (at the end of executing app) if you want to use them elsewhere:

```
app(\SlyDeath\NestedCaching\CacheStack::class)->getAnotherCaches();
```

### How to clear the stack of another caches?

[](#how-to-clear-the-stack-of-another-caches)

Just call`clearAnotherCaches` method:

```
 app(\SlyDeath\NestedCaching\CacheStack::class)->clearAnotherCaches();
```

### How to save keys for different pages?

[](#how-to-save-keys-for-different-pages)

You can generate dynamically the cache key (for example from the page id):

```
app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->getAnotherCaches();
```

### How to clear the stack of another caches with custom cache key?

[](#how-to-clear-the-stack-of-another-caches-with-custom-cache-key)

Just call`clearAnotherCaches` method and provide the key:

```
 app(\SlyDeath\NestedCaching\CacheStack::class)->setAnotherKey('my-cache-key-prefix:' . optional($page)->id)->clearAnotherCaches();
```

Enable PhpStorm support
-----------------------

[](#enable-phpstorm-support)

Go to the `Settings → PHP → Blade`, then uncheck **Use default settings**. Go to **Directives** tab and press «+» to add another one custom directive:

- Name: `cache`
- Checkbox **Has parameters** → `true`
- Prefix: ``

And add close directive without parameters:

- Name: `endcache` or `endCache`, whatever you use

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

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

Recently: every ~176 days

Total

6

Last Release

1055d ago

Major Versions

1.3 → 2.02022-11-15

2.0 → 3.02023-06-20

PHP version history (2 changes)1.0PHP ^7.4|^8.0

2.0PHP ^7.4|^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/3569b9f649cad0bd9adf91a6bb74b35ff58108505154ab8598f92108ca6218e8?d=identicon)[SlyDeath](/maintainers/SlyDeath)

---

Top Contributors

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

---

Tags

cachecache-controllaravellaravel-frameworklaravel-packagephp

### Embed Badge

![Health badge](/badges/slydeath-laravel-nested-caching/health.svg)

```
[![Health](https://phpackages.com/badges/slydeath-laravel-nested-caching/health.svg)](https://phpackages.com/packages/slydeath-laravel-nested-caching)
```

###  Alternatives

[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)[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)
