1. Go to this page and download the library: Download maher/laravel-counters 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/ */
maher / laravel-counters example snippets
//increment/decrement system counters
Counters::increment('number_of_visitors');
// increment/decrement model objects counters
$post->incrementCounter('number_of_views');
$feature->decrementCounter('number_of_bugs');
$user->decrementCounter('balance', 2); // decrement the balance of the user by 2
use Maher\Counters\Traits\HasCounter;
class Post extends Model
{
use HasCounter;
// ...
}
use Maher\Counters\Models\Counter;
$counter = Counter([
'key' => 'number_of_views',
'name' => 'Views',
'initial_value' => 0 //(could be left empty, default value is 0)
'step' => 1 // (could be left empty, default value is 1)
]);
class PostsController extends Controller
{
public function show(Post $post)
{
//...
$post->incrementCounter('number_of_views');
//...
}
}
// will return the counter object
$post->getCounter($key);
// will return the counter value
$post->getCounterValue($key);
//will add record in countrable table for this post object
$post->addCounter($key);
//will remove the record from countrable table for this post object.
$post->removeCounter($key);
//increment the counter with the given $key
//Note that this will create record in countrable table,if it's not exist
//if $step is entered, it will increment with the value of $step
$post->incrementCounter($key, $step = null);
//decrement the counter with the given $key
//Note that this will create record in countrable table,if it's not exist
//if $step is entered, it will decrement with the value of $step
$post->decrementCounter($key, $step = null);
// will reset the counter value (to initial_value) for the post object.
$post->resetCounter($key);
use Maher\Counters\Facades\Counters;
class Test
{
public function incrementFunction()
{
//moreover you can add this function in your public page to be incremented
//every time user hits your website
Counters::increment('number_of_visitors');
}
}
// will return the counter object
Counters::get($key);
// will return the counter value
Counters::getValue($key, $default = null);
// set the value of the counter
Counters::setValue($key, $value);
//set the step of the counter
Counters::setStep($key, $step);
//increment the counter with the given $key
Counters::increment($key, $step = null);
//decrement the counter with the given $key
Counters::decrement($key, $step = null);
// will reset the counter for the inital_value
Counters::reset($key);
use Maher\Counters\Facades\Counters;
//this will return a link to increment the counter key
//for exampel 'exmple.com/counters/increment/visitors'
Counters::getIncrementUrl($key);
Counters::getDecrementUrl($key);
// we can use these in Blades for example,
// <a href={{\Counters::getIncrementUrl($key)}}>Incrment Visitors</a>
use Illuminate\Database\Seeder;
use Maher\Counters\Facades\Counters;
class CounterTableSeeder extends Seeder
{
public function run()
{
// create Counters
//This will create a counter with inital value as 3, and every increment 5 will be added.
Counter::create([
'key' => 'number_of_visitors',
'name' => 'Visitors',
'initial_value' => 3,
'step' => 5
]);
//This counter will has 0 as inital_value and 1 as step
Counter::create([
'key' => 'number_of_visitors2',
'name' => 'Visitors2'
]);
$viewCounter = Counter::create([
'key' => 'number_of_views',
'name' => 'Views'
]);
$post = Post::find(1);
$post->addCounter('number_of_views');// to add the record to countrable table
}
}