Download the PHP package turahe/laravel-counters without Composer
On this page you can find all versions of the php package turahe/laravel-counters. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download turahe/laravel-counters
More information about turahe/laravel-counters
Files in turahe/laravel-counters
Package laravel-counters
Short Description management for counters in laravel system
License MIT
Homepage https://github.com/turahe/laravel-counters
Informations about the package laravel-counters
Laravel Counters
A modern, optimized counter management package for Laravel 11/12/13 with PHP 8.4 and 8.5 support.
A flexible and powerful counter management system for Laravel applications. Easily track and manage various types of counters like page views, downloads, user actions, and more without cluttering your database schema.
🚀 Features
- ✅ PHP 8.4 & 8.5: Readonly properties, constructor property promotion, match expressions, and strict typing
- ✅ Laravel 11/12/13 Compatible: Modern service provider patterns and dependency injection
- ✅ High Performance: Built-in caching, bulk operations, and optimized database queries
- ✅ Type Safe: Full type declarations throughout
- ✅ Comprehensive Testing: Full test suite with Codecov coverage reporting
- Model-specific counters: Associate counters with any Eloquent model
- Global counters: System-wide counters for general statistics
- Cookie-based tracking: Prevent duplicate increments from the same user
- Flexible configuration: Customizable table names and settings
- Artisan commands: Create counters via command line
📋 Table of Contents
- Installation
- Quick Start
- Usage
- Model Counters
- Global Counters
- Cookie-based Tracking
- API Reference
- Configuration
- Testing
- Contributing
Installation
Requirements
- PHP: 8.4 or higher (8.5 supported)
- Laravel: 11.x, 12.x, or 13.x
Step-by-Step Installation
-
Install the package via Composer:
- Publish the configuration and migrations:
This will publish:
- Configuration file:
config/counter.php - Migration file:
database/migrations/xxxx_xx_xx_xxxxxx_create_counters_tables.php
- Run the migrations:
This creates the counters and counterables tables in your database.
Quick Start
1. Create a Counter
2. Use with Models
3. Track Views
4. Global Counters
Usage
Model Counters
Add the HasCounter trait to any model you want to track:
Available Methods
Global Counters
Use the Counters facade for system-wide counters:
Cookie-based Tracking
Prevent duplicate increments from the same user:
API Reference
Model Methods (HasCounter Trait)
| Method | Description | Parameters |
|---|---|---|
addCounter($key, $initialValue = null) |
Add counter to model | $key: Counter key, $initialValue: Optional initial value |
getCounter($key) |
Get counter object | $key: Counter key |
getCounterValue($key) |
Get counter value | $key: Counter key |
hasCounter($key) |
Check if model has counter | $key: Counter key |
incrementCounter($key, $step = null) |
Increment counter | $key: Counter key, $step: Optional step value |
decrementCounter($key, $step = null) |
Decrement counter | $key: Counter key, $step: Optional step value |
resetCounter($key, $initialValue = null) |
Reset counter | $key: Counter key, $initialValue: Optional reset value |
removeCounter($key) |
Remove counter from model | $key: Counter key |
Global Counter Methods (Counters Facade)
| Method | Description | Parameters |
|---|---|---|
create($key, $name, $initialValue = 0, $step = 1) |
Create a counter | $key: Counter key, $name: Display name, $initialValue: Initial value, $step: Step value |
get($key) |
Get counter object | $key: Counter key |
getValue($key, $default = null) |
Get counter value | $key: Counter key, $default: Default value if not found |
setValue($key, $value) |
Set counter value | $key: Counter key, $value: New value |
setStep($key, $step) |
Set counter step | $key: Counter key, $step: Step value |
increment($key, $step = null) |
Increment counter | $key: Counter key, $step: Optional step value |
decrement($key, $step = null) |
Decrement counter | $key: Counter key, $step: Optional step value |
reset($key) |
Reset counter | $key: Counter key |
incrementIfNotHasCookies($key) |
Increment if no cookie | $key: Counter key |
decrementIfNotHasCookies($key) |
Decrement if no cookie | $key: Counter key |
Configuration
The package configuration is located at config/counter.php:
Customizing Table Names
You can customize the table names in the configuration:
Artisan Commands
Create Counter
Create a counter via command line:
Parameters:
page_views: Counter key"Page Views": Display name0: Initial value1: Step value
Testing
The package includes a comprehensive test suite covering model counters, global counters, cookie-based tracking, exceptions, and configuration.
Local
Generate an HTML coverage report locally:
Docker
Run tests in a container (default: PHP 8.5 + Laravel 13):
Makefile
Run the full CI matrix (PHP 8.4/8.5 × Laravel 11/12/13):
Coverage
Coverage is uploaded to Codecov on every push and pull request via the Codecov workflow.
What's tested
- ✅ Model counter operations
- ✅ Global counter operations
- ✅ Cookie-based tracking
- ✅ Exception handling
- ✅ Database operations
- ✅ Configuration flexibility
Common Usage Patterns
✅ Best Practices
-
Always create counters before using them:
-
Use meaningful counter keys:
- Handle counter existence gracefully:
❌ Common Mistakes
-
Don't forget to run migrations:
- Don't use counters without creating them first:
Performance Optimizations
Caching
The package includes built-in caching for counter lookups, significantly improving performance for frequently accessed counters.
Bulk Operations
Use bulk operations to update multiple counters efficiently:
Database Indexes
The migration includes optimized indexes for better query performance.
PHP 8.4+ Features Used
- Readonly Properties: Immutable data structures
- Constructor Property Promotion: Cleaner class definitions
- Match Expressions: Modern control flow
- Named Arguments: Self-documenting function calls
- Improved Type Declarations: Better type safety
- Strict Types: Enforced type checking
Laravel 11/12/13 Features Used
- Modern Service Providers: Deferrable providers for better performance
- Improved Dependency Injection: Constructor injection and type hints
- Enhanced Model Features: Better relationships and scopes
- Modern Command Structure: Improved Artisan commands
Example Seeder
Example seeder for creating counters:
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This package is open-sourced software licensed under the MIT license.
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Made with ❤️ by Nur Wachid
All versions of laravel-counters with dependencies
illuminate/support Version ^11.0 || ^12.0 || ^13.0
illuminate/database Version ^11.0 || ^12.0 || ^13.0