Download the PHP package maher/laravel-counters without Composer
On this page you can find all versions of the php package maher/laravel-counters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-counters
Counters Management for laravel project.
- Installation
- Usage
- 1) Using Counters Associated with model
- 2) Using Counters with no models
- 3) Using artisan commands
- 4) Using APIs for counters
- Database Seeding
- Cache[To be added later]
- Credits
- Special Thanks
- license
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:
There are many other methods that are mentioned below.
Installation
- Laravel
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:
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:
You must publish the migration with:
After the migration has been published you can create the tables by running the migrations:
You can publish the config file with:
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
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.
After that, for example, in the show function of post controller, you can add this line:
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.
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.
This Facade has many other functions:
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:
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
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
by Using this link, a json structure of the updated counter will return as response
Moreover, we can increment/decrement objects counters by these methods
And a json structure like the following will return as response
Database Seeding
Here's a sample seede.
Credits
Maher Khdeir
Linked In
Email: [email protected]
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.