Download the PHP package climactic/laravel-credits without Composer
On this page you can find all versions of the php package climactic/laravel-credits. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download climactic/laravel-credits
More information about climactic/laravel-credits
Files in climactic/laravel-credits
Package laravel-credits
Short Description A ledger-based Laravel package for managing credit-based systems in your application.
License MIT
Homepage https://github.com/climactic/laravel-credits
Informations about the package laravel-credits
๐ Table of Contents
- Laravel Credits
- ๐ Table of Contents
- Features
- ๐ Sponsors
- ๐ฆ Installation
- โ๏ธ Configuration
- ๐๏ธ Database Recommendations
- ๐ Usage
- ๐ง Setup Your Model
- ๐ณ Basic Usage
- ๐ธ Transfers
- ๐ Transaction History
- ๐ Historical Balance
- ๐ Metadata
- ๐ Querying by Metadata
- Metadata Key Format
- Basic Metadata Queries
- Advanced Metadata Queries
- Chaining Multiple Metadata Conditions
- Convenience Methods
- โก Performance Optimization
- When to Optimize
- MySQL/MariaDB: Virtual Columns with Indexes
- PostgreSQL: GIN Indexes on JSONB
- SQLite: Limited Support
- Choosing What to Index
- Best Practices
- ๐ข Events
- ๐ API Reference
- ๐ง Available Methods
- ๐ Query Scopes
- โ ๏ธ Deprecated Methods
- ๐งช Testing
- ๐ Changelog
- ๐ค Contributing
- ๐ Security Vulnerabilities
- ๐ Support This Project
- โญ Star History
- ๐ License
- โ๏ธ Disclaimer
Features
- ๐ Credit transactions
- ๐ธ Credit transfers
- ๐ข Events for adding, deducting, and transferring credits
- ๐ฐ Balance tracking with running balance
- ๐ Transaction history
- ๐ Point-in-time balance lookup
- ๐ Transaction metadata support
- ๐ Powerful metadata querying with filters and scopes
- โก Efficient queries using running balance and indexes
- ๐ Performance optimization guide for high-volume applications
๐ Sponsors
Your logo here โ Become a sponsor and get your logo featured in this README and on our website.
Interested in title sponsorship? Contact us at [email protected] for premium placement and recognition.
๐ฆ Installation
You can install the package via composer:
Publish and run the migrations:
Optionally publish the config file:
โ๏ธ Configuration
๐๏ธ Database Recommendations
Concurrency & Locking: This package uses row-level locking (SELECT FOR UPDATE) to prevent race conditions during concurrent credit operations. This requires a database engine that supports proper transaction isolation and row-level locking:
- โ MySQL/MariaDB: Requires InnoDB engine (default in modern versions)
- โ PostgreSQL: Full support for row-level locking
- โ ๏ธ SQLite: Row-level locking is ignored; concurrent operations may produce incorrect results in high-concurrency scenarios
For production environments with concurrent users, we recommend using MySQL/MariaDB (InnoDB) or PostgreSQL.
๐ Usage
๐ง Setup Your Model
Add the HasCredits trait to any model that should handle credits:
๐ณ Basic Usage
๐ธ Transfers
Transfer credits between two models:
๐ Transaction History
๐ Historical Balance
Get balance as of a specific date:
๐ Metadata
Add additional information to transactions:
๐ Querying by Metadata
The package provides powerful query scopes to filter transactions by metadata with built-in input validation for security.
Metadata Key Format
Metadata keys must follow these rules:
- Use dot notation for nested keys (e.g.,
'user.id', not'user->id') - Cannot be empty or contain only whitespace
- Cannot contain quotes (
"or') - Whitespace is automatically trimmed
Basic Metadata Queries
Advanced Metadata Queries
Chaining Multiple Metadata Conditions
Convenience Methods
Note: When using creditHistoryWithMetadata(), the filter array distinguishes between "two-parameter syntax" and "three-parameter syntax" by checking if the 'value' key exists:
- If
'value'key exists: uses three-parameter formwhereMetadata($key, $operator, $value) - If
'value'key missing: uses two-parameter formwhereMetadata($key, $operator)where operator becomes the value
This allows proper handling of null values while maintaining shorthand syntax convenience.
โก Performance Optimization
For high-volume applications querying metadata frequently, consider adding database indexes. Without indexes, metadata queries perform full table scans. With proper indexes, queries become nearly instant even with millions of records.
When to Optimize
- Small datasets (< 10k transactions): No optimization needed
- Medium datasets (10k - 100k): Consider optimization for frequently queried keys
- Large datasets (> 100k): Highly recommended for any metadata queries
MySQL/MariaDB: Virtual Columns with Indexes
Virtual generated columns extract JSON values into indexed columns for fast queries:
Performance impact: Queries go from scanning millions of rows to using index lookups (1000x+ faster).
PostgreSQL: GIN Indexes on JSONB
PostgreSQL's GIN (Generalized Inverted Index) provides efficient querying for all JSON operations:
Note: The GIN index alone enables fast queries for all metadata keys. Path-specific indexes provide marginal additional performance.
SQLite: Limited Support
SQLite has limited JSON indexing capabilities. For SQLite:
- Metadata queries work but will be slower on large datasets
- Consider using a different database for production if metadata querying is critical
- JSON1 extension must be enabled (available in SQLite 3.38+)
Choosing What to Index
Index metadata keys that you query frequently:
Best Practices
- Analyze your queries first: Use
EXPLAINto identify slow queries - Index selectively: Only index frequently queried keys (each index adds storage overhead)
- Use composite indexes: For queries combining metadata with other columns
- Test with production data: Benchmark before and after indexing
- Monitor index usage: Remove unused indexes to save storage
๐ข Events
Events are fired for each credit transaction, transfer, and balance update.
The events are:
CreditsAddedCreditsDeductedCreditsTransferred
๐ API Reference
๐ง Available Methods
| Method | Description |
|---|---|
creditAdd(float $amount, ?string $description = null, array $metadata = []) |
Add credits to the model |
creditDeduct(float $amount, ?string $description = null, array $metadata = []) |
Deduct credits from the model |
creditBalance() |
Get the current balance |
creditTransfer(Model $recipient, float $amount, ?string $description = null, array $metadata = []) |
Transfer credits to another model |
creditHistory(int $limit = 10, string $order = 'desc') |
Get transaction history |
hasCredits(float $amount) |
Check if model has enough credits |
creditBalanceAt(Carbon\|DateTimeInterface\|int $dateTime) |
Get balance at a specific time |
credits() |
Eloquent relationship to credit transactions |
creditsByMetadata(string $key, $operator, $value = null, int $limit = 10, string $order = 'desc') |
Get credits filtered by metadata key/value |
creditHistoryWithMetadata(array $filters, int $limit = 10, string $order = 'desc') |
Get credits filtered by multiple metadata |
๐ Query Scopes
These scopes can be used on the credits() relationship:
| Scope | Description |
|---|---|
whereMetadata(string $key, $operator, $value = null) |
Filter by metadata key/value |
whereMetadataContains(string $key, $value) |
Filter where metadata array contains value |
whereMetadataHas(string $key) |
Filter where metadata key exists |
whereMetadataNull(string $key) |
Filter where metadata key is null/doesn't exist |
whereMetadataLength(string $key, $operator, $value) |
Filter by metadata array length |
โ ๏ธ Deprecated Methods
The following methods are deprecated and will be removed in v2.0. They still work but will trigger deprecation warnings:
| Deprecated Method | Use Instead |
|---|---|
addCredits() |
creditAdd() |
deductCredits() |
creditDeduct() |
getCurrentBalance() |
creditBalance() |
transferCredits() |
creditTransfer() |
getTransactionHistory() |
creditHistory() |
hasEnoughCredits() |
hasCredits() |
getBalanceAsOf() |
creditBalanceAt() |
creditTransactions() |
credits() |
๐งช Testing
๐ Changelog
Please see CHANGELOG for more information on what has changed recently.
๐ค Contributing
Please see CONTRIBUTING for details. You can also join our Discord server to discuss ideas and get help: Discord Invite.
๐ Security Vulnerabilities
Please report security vulnerabilities to [email protected].
๐ Support This Project
Laravel Credits is free and open source, built and maintained with care. If this package has saved you development time or helped power your application, please consider supporting its continued development.
โญ Star History
๐ License
The MIT License (MIT). Please see License File for more information.
โ๏ธ Disclaimer
This package is not affiliated with Laravel. It's for Laravel but is not by Laravel. Laravel is a trademark of Taylor Otwell.
All versions of laravel-credits with dependencies
spatie/laravel-package-tools Version ^1.92.7
illuminate/contracts Version ^12.43.1 || ^13.1