1. Go to this page and download the library: Download kanazaca/counter-cache library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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();
});
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');
}
}
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;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.