2017 © Pedro Peláez
 

library counter-cache

Package for Laravel to implement counter cache

image

kanazaca/counter-cache

Package for Laravel to implement counter cache

  • Monday, October 23, 2017
  • by kanazaca
  • Repository
  • 2 Watchers
  • 11 Stars
  • 2,604 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 3 Versions
  • 19 % Grown

The README.md

Counter Cache for Laravel - Updated 11/10/2016

Counter Cache for Laravel, (*1)

Why ?

Imagine if you have to show 50 products in a list and have to show a counter for how many comments that product have, too much queries, right ? This package will allow you to super reduce the number of queries made., (*2)

Feature Overview

  • Increment counter automatically when creating a new record.
  • Decrement counter automatically when deleting a record.
  • Update counter automatically when updating a record
  • ...

Installation

Add this to your composer.json file, in the require object:, (*3)

"kanazaca/counter-cache": "1.0.*"

After that, run composer install to install the package. Add the service provider to config/app.php, within the providers array., (*4)

'providers' => array(
    // ...
    kanazaca\CounterCache\CounterCacheServiceProvider::class,
)

Basic Usage

I will use the example products/comments, one product have many comments, (*5)

Migration

You need to create a field in the table that you want access the counter, like the example below:, (*6)

Schema::create('products', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name');
      $table->string('ref');
      $table->integer('comments_count')->default(0); // This is the counter that you have to add
      $table->timestamps();
  });

After this run php artisan migrate, (*7)

Model

Comments model, you have to use the trait, define the $counterCacheOptions and make the relation with the product :, (*8)

namespace App;

use Illuminate\Database\Eloquent\Model;
use kanazaca\CounterCache\CounterCache;

class Comments extends Model
{
    use CounterCache;

    // you can have more than one counter
    public $counterCacheOptions = [
        'Product' => ['field' => 'comments_count', 'foreignKey' => 'product_id']
    ];

    public function Product()
    {
        return $this->belongsTo('App\Product');
    }
}

Filters

If you want to do some filtering before the counter cache magic happens, you have to add the key filter to $counterCacheOptions with the name of your method that will provide the filter, as string, like below:, (*9)

public $counterCacheOptions = [
    'Product' => [
        'field' => 'comments_count',
        'foreignKey' => 'product_id',
        'filter' => 'CommentValidatedFilter'
    ]
]; // you can have more than one counter

// this code will be executed before the counting (save and update method)
public function CommentValidatedFilter()
{
    if ($this->validated) {
        return true;
    }

    return false;
}

Credits

Hugo Neto, (*10)

The Versions

23/10 2017

dev-master

9999999-dev

Package for Laravel to implement counter cache

  Sources   Download

MIT

by Hugo Neto

25/05 2016

1.0.1

1.0.1.0

Package for Laravel to implement counter cache

  Sources   Download

MIT

by Hugo Neto

17/12 2015

1.0.0

1.0.0.0

Package for Laravel to implement counter cache

  Sources   Download

MIT

by Hugo Neto