quankim/laravel-counter-cache

Counter Cache for Laravel 5

1.2 2017-12-13 16:45 UTC

This package is not auto-updated.

Last update: 2024-12-22 05:39:46 UTC


README

Package Counter Cache for Laravel 5

Latest Stable Version Total Downloads Latest Unstable Version License

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

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 Product:

<?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:

<?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:

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