管理 Laravel 中的信用系统
Laravel 的 Credits 包是一个基于账本的 Laravel 扩展包,用于管理应用中的信用系统。适用于虚拟货币、奖励积分或任何基于信用的特性:
use Climactic\Credits\Traits\HasCredits;
class User extends Model
{
use HasCredits;
}
// Basic usage
// Add credits
$user->creditAdd(100.00, 'Subscription Activated');
// Deduct credits
$user->creditDeduct(50.00, 'Purchase Made');
// Get current balance
$balance = $user->creditBalance();
// Check if user has enough credits
if ($user->hasCredits(30.00)) {
// Proceed with transaction
}有了这个包,你可以转账、查询交易记录、历史余额等:
$sender->creditTransfer($recipient, 100.00, 'Paying to user for their service');
// Get last 10 transactions
$history = $user->creditHistory();
// Get last 20 transactions in ascending order
$history = $user->creditHistory(20, 'asc');
$balanceAsOf = $user->creditBalanceAt(new DateTime('2023-01-01'));主特性
- 交易
- 转账
- 用于增加余额、扣减余额和转账的事件
- 运行时余额进行余额跟踪
- 交易历史记录
- 时间点余额查询
- 事务元数据支持
- 使用运行余额和索引进行高效查询
💻 GitHub: Climactic/laravel-credits