Download the PHP package kanazaca/counter-cache without Composer
On this page you can find all versions of the php package kanazaca/counter-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kanazaca/counter-cache
More information about kanazaca/counter-cache
Files in kanazaca/counter-cache
Package counter-cache
Short Description Package for Laravel to implement counter cache
License MIT
Informations about the package counter-cache
Counter Cache for Laravel - Updated 11/10/2016
Counter Cache for Laravel
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.
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:
After that, run composer install to install the package.
Add the service provider to config/app.php
, within the providers
array.
Basic Usage
I will use the example products/comments, one product have many comments
Migration
You need to create a field in the table that you want access the counter, like the example below:
After this run php artisan migrate
Model
Comments model, you have to use the trait, define the $counterCacheOptions and make the relation with the 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:
Credits
Hugo Neto