项目作者: MaherSaleem

项目描述 :
Counters Management for laravel project.
高级语言: PHP
项目地址: git://github.com/MaherSaleem/laravel-counters.git
创建时间: 2018-06-20T19:29:24Z
项目社区:https://github.com/MaherSaleem/laravel-counters

开源协议:MIT License

下载


Counters Management for laravel project.

In some cases, you need to manage the state of the counters in your laravel project, like the number of visitors of your website,
or number of view for a post, or number of downloads for a file, this needs to create a new table to save these records,
or at least adding new column for your tables to save the count value.
Therefore, with this package, you can create as many counters for your model without needing to create a physical column in your model’s table.
Moreover, you can store public counters like “number_of_visitors” for your website, without needing to create a whole table to store it.

In summary, this package allows you to manage counters in your laravel project.

Once installed you can do stuff like this:

  1. //increment/decrement system counters
  2. Counters::increment('number_of_visitors');
  3. // increment/decrement model objects counters
  4. $post->incrementCounter('number_of_views');
  5. $feature->decrementCounter('number_of_bugs');
  6. $user->decrementCounter('balance', 2); // decrement the balance of the user by 2

There are many other methods that are mentioned below.

Installation

Laravel

This package can be used in Laravel 5.4 or higher. If you are using an older version of Laravel You can install the package via composer:

  1. composer require maher/laravel-counters "@dev"

In Laravel 5.5 and higher versions, the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

  1. 'providers' => [
  2. // ...
  3. Maher\Counters\CountersServiceProvider::class,
  4. //...
  5. ];

You must publish the migration with:

  1. php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="migrations"

After the migration has been published you can create the tables by running the migrations:

  1. php artisan migrate

You can publish the config file with:

  1. php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="config"

Usage

1) Using Counters with no models

First, add the Maher\Counters\Traits\HasCounter trait to your model(s):
for example we can add it to Post Model

  1. use Maher\Counters\Traits\HasCounter;
  2. class Post extends Model
  3. {
  4. use HasCounter;
  5. // ...
  6. }

This package allows the posts to be associated with counters. Every post can associated with multiple counters.

We can create a counter like this, for example, lets create a counter for the number of views for the Post Model.

  1. use Maher\Counters\Models\Counter;
  2. $counter = Counter([
  3. 'key' => 'number_of_views',
  4. 'name' => 'Views',
  5. 'initial_value' => 0 //(could be left empty, default value is 0)
  6. 'step' => 1 // (could be left empty, default value is 1)
  7. ]);

After that, for example, in the show function of post controller, you can add this line:

  1. class PostsController extends Controller
  2. {
  3. public function show(Post $post)
  4. {
  5. //...
  6. $post->incrementCounter('number_of_views');
  7. //...
  8. }
  9. }

By doing this, the counter of number_of_views for every post will be incremented [by the step size] as we show the post.

This package has another functions.

  1. // will return the counter object
  2. $post->getCounter($key);
  3. // will return the counter value
  4. $post->getCounterValue($key);
  5. //will add record in countrable table for this post object
  6. $post->addCounter($key);
  7. //will remove the record from countrable table for this post object.
  8. $post->removeCounter($key);
  9. //increment the counter with the given $key
  10. //Note that this will create record in countrable table,if it's not exist
  11. //if $step is entered, it will increment with the value of $step
  12. $post->incrementCounter($key, $step = null);
  13. //decrement the counter with the given $key
  14. //Note that this will create record in countrable table,if it's not exist
  15. //if $step is entered, it will decrement with the value of $step
  16. $post->decrementCounter($key, $step = null);
  17. // will reset the counter value (to initial_value) for the post object.
  18. $post->resetCounter($key);

2) Using Counters with no models.

Sometimes, you have general counters that are not associated with any models, for example the number visitor for your website.

Therefore, this package will allow you to deal with Counter with these types.

  1. use Maher\Counters\Facades\Counters;
  2. class Test
  3. {
  4. public function incrementFunction()
  5. {
  6. //moreover you can add this function in your public page to be incremented
  7. //every time user hits your website
  8. Counters::increment('number_of_visitors');
  9. }
  10. }

This Facade has many other functions:

  1. // will return the counter object
  2. Counters::get($key);
  3. // will return the counter value
  4. Counters::getValue($key, $default = null);
  5. // set the value of the counter
  6. Counters::setValue($key, $value);
  7. //set the step of the counter
  8. Counters::setStep($key, $step);
  9. //increment the counter with the given $key
  10. Counters::increment($key, $step = null);
  11. //decrement the counter with the given $key
  12. Counters::decrement($key, $step = null);
  13. // will reset the counter for the inital_value
  14. Counters::reset($key);

In some cases, you want to increment the counter once for every person, for example no need to increment the number_of_visitors counter every time the same user refreshes the page.

So you can use these functions:

  1. Counters::incrementIfNotHasCookies($key);
  2. Counters::decrementIfNotHasCookies($key);

3)Using artisan commands

You can create a Counter from a console with artisan commands.
The following command creates the counter number_of_visitors with initial value 0 and step 1

  1. php artisan make:counter number_of_visitors Visitors 0 1

4) Using APIs for counters

In some cases, we are using a single page application, or we don’t want to leave the current page. Therefore
there are APIs to increment/decrement general/per_model_object counters.

Examples

  1. use Maher\Counters\Facades\Counters;
  2. //this will return a link to increment the counter key
  3. //for exampel 'exmple.com/counters/increment/visitors'
  4. Counters::getIncrementUrl($key);
  5. Counters::getDecrementUrl($key);
  6. // we can use these in Blades for example,
  7. // <a href={{\Counters::getIncrementUrl($key)}}>Incrment Visitors</a>

by Using this link, a json structure of the updated counter will return as response

  1. {
  2. "counter": {
  3. "id": 1,
  4. "key": "visitors",
  5. "name": "Visitors",
  6. "initial_value": 0,
  7. "value": 9,
  8. "step": 3,
  9. "notes": null,
  10. "created_at": "2018-07-02 20:57:03",
  11. "updated_at": "2018-07-07 13:07:49"
  12. }
  13. }

Moreover, we can increment/decrement objects counters by these methods

  1. $post = Post::find(1);
  2. $post->getIncrementUrl($key);
  3. $post->getDecrementUrl($key);

And a json structure like the following will return as response

  1. {
  2. "counterable": {
  3. "id": 2,
  4. "counterable_id": 2,
  5. "counterable_type": "App\\Post",
  6. "counter_id": 1,
  7. "value": 24,
  8. "created_at": null,
  9. "updated_at": "2018-07-07 13:09:41",
  10. "counter": {
  11. "id": 1,
  12. "key": "visitors",
  13. "name": "Visitors",
  14. "initial_value": 0,
  15. "value": 9,
  16. "step": 3,
  17. "notes": null,
  18. "created_at": "2018-07-02 20:57:03",
  19. "updated_at": "2018-07-07 13:07:49"
  20. }
  21. }
  22. }

Database Seeding

Here’s a sample seede.

  1. use Illuminate\Database\Seeder;
  2. use Maher\Counters\Facades\Counters;
  3. class CounterTableSeeder extends Seeder
  4. {
  5. public function run()
  6. {
  7. // create Counters
  8. //This will create a counter with inital value as 3, and every increment 5 will be added.
  9. Counter::create([
  10. 'key' => 'number_of_visitors',
  11. 'name' => 'Visitors',
  12. 'initial_value' => 3,
  13. 'step' => 5
  14. ]);
  15. //This counter will has 0 as inital_value and 1 as step
  16. Counter::create([
  17. 'key' => 'number_of_visitors2',
  18. 'name' => 'Visitors2'
  19. ]);
  20. $viewCounter = Counter::create([
  21. 'key' => 'number_of_views',
  22. 'name' => 'Views'
  23. ]);
  24. $post = Post::find(1);
  25. $post->addCounter('number_of_views');// to add the record to countrable table
  26. }
  27. }

Credits

Maher Khdeir
Linked In
Email: maher.khdeir@gmail.com

Special Thanks

Special Thanks to Spatie , since I learned and followed there structure of building laravel packages.

License

The MIT License (MIT). Please see License File for more information.