2017 © Pedro Peláez
 

library laravel-counter-cache

Counter Cache for Laravel 5

image

quankim/laravel-counter-cache

Counter Cache for Laravel 5

  • Wednesday, December 13, 2017
  • by quanvh
  • Repository
  • 0 Watchers
  • 4 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 4 Versions
  • 25 % Grown

The README.md

Laravel Package: Counter Cache

Package Counter Cache for Laravel 5, (*1)

Latest Stable Version Total Downloads Latest Unstable Version License, (*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
  • Update rating_average automatically when CRUD a record
  • Custom field when CRUD a record

Install

composer require quankim/laravel-counter-cache

Usage

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

Migration

Table products:, (*4)

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:, (*5)

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 Product:, (*6)

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Comment;

class Product extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function ratingAverage()
    {
        return round($this->comments()->avg('rating_value'), 1);
    }
}

Model Comment:, (*7)

<?php
namespace App\Models;

use QuanKim\LaravelCounterCache\Traits\CounterCache;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product;

class Comment extends Model
{
    use CounterCache;

    public $counterCacheOptions = [
        'product' => [
            'comments_count' => [],
            'rating_average' => [
                'method' => 'ratingAverage',
            ],
        ],
    ];

    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}

if you use boot() function in Model Comment, you must define as below:, (*8)

use CounterCache {
    boot as preBoot;
}

protected static function boot()
{
    self::preBoot();

    // ...
}

Add Conditions

public $counterCacheOptions = [
    'product' => [
        'comments_count' => [],
        'rating_average' => [
            'method' => 'ratingAverage',
            'conditions' => [
                'is_publish' => true,
            ],
        ],
    ],
];

Credits

Vo Hong Quan, (*9)

The Versions

13/12 2017

dev-master

9999999-dev

Counter Cache for Laravel 5

  Sources   Download

MIT

by Vo Hong Quan

13/12 2017

1.2

1.2.0.0

Counter Cache for Laravel 5

  Sources   Download

MIT

by Vo Hong Quan

29/10 2017

1.1

1.1.0.0

Counter Cache for Laravel 5

  Sources   Download

MIT

by Vo Hong Quan

29/10 2017

1.0

1.0.0.0

Counter Cache for Laravel 5

  Sources   Download

MIT

by Vo Hong Quan