PHPackages                             quankim/laravel-counter-cache - 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. quankim/laravel-counter-cache

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

quankim/laravel-counter-cache
=============================

Counter Cache for Laravel 5

1.2(8y ago)5482[1 PRs](https://github.com/quanvhframgia/laravel-counter-cache/pulls)MITPHP

Since Oct 29Pushed 8y agoCompare

[ Source](https://github.com/quanvhframgia/laravel-counter-cache)[ Packagist](https://packagist.org/packages/quankim/laravel-counter-cache)[ RSS](/packages/quankim-laravel-counter-cache/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Laravel Package: Counter Cache
==============================

[](#laravel-package-counter-cache)

Package Counter Cache for Laravel 5

[![Latest Stable Version](https://camo.githubusercontent.com/c5e917cc5fc948815571dc4fa6d3680b6fee8bdc8cefaf7c3730fe2e51fd617c/68747470733a2f2f706f7365722e707567782e6f72672f7175616e6b696d2f6c61726176656c2d636f756e7465722d63616368652f762f737461626c65)](https://packagist.org/packages/quankim/laravel-counter-cache)[![Total Downloads](https://camo.githubusercontent.com/a4f053ebb8ac23671595a192db43f074e159208a8422c0cb314bb33053381e43/68747470733a2f2f706f7365722e707567782e6f72672f7175616e6b696d2f6c61726176656c2d636f756e7465722d63616368652f646f776e6c6f616473)](https://packagist.org/packages/quankim/laravel-counter-cache)[![Latest Unstable Version](https://camo.githubusercontent.com/f4878c6a5ed1eacb233d889f75bdec25682a30287448f2443973b72a79af1d25/68747470733a2f2f706f7365722e707567782e6f72672f7175616e6b696d2f6c61726176656c2d636f756e7465722d63616368652f762f756e737461626c65)](https://packagist.org/packages/quankim/laravel-counter-cache)[![License](https://camo.githubusercontent.com/d76ac4e04ade69f0deb9bb23cb5db4cb1b2bd9232584f7e647abf30fc8fd9867/68747470733a2f2f706f7365722e707567782e6f72672f7175616e6b696d2f6c61726176656c2d636f756e7465722d63616368652f6c6963656e7365)](https://packagist.org/packages/quankim/laravel-counter-cache)

Feature Overview
----------------

[](#feature-overview)

- Increment counter automatically when creating a new record.
- Decrement counter automatically when deleting a record.
- Update counter automatically when updating a record
- Update rating\_average automatically when CRUD a record
- Custom field when CRUD a record

Install
-------

[](#install)

```
composer require quankim/laravel-counter-cache
```

Usage
=====

[](#usage)

I will use the example products/comments, one product have many comments

### Migration

[](#migration)

Table products:

```
Schema::create('products', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');
      $table->integer('comments_count')->default(0);
      $table->float('rating_average', 15, 1)->nullable();
      $table->timestamps();
  });
```

Table comments:

```
Schema::create('comments', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('product_id');
      $table->string('content');
      $table->integer('rating_value')->nullable();
      $table->timestamps();
  });
```

### Model

[](#model)

Model Product:

```
