mprince/laravel-pointable

Laravel 8 的积分系统

1.0 2021-02-28 05:22 UTC

This package is auto-updated.

Last update: 2024-09-04 14:44:28 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel Pointable

Laravel 8.* 的积分交易系统

灵感来自 Trexology

安装

首先,使用 Composer 拉取包。

Laravel 8

composer require mprince/laravel-pointable

Laravel 7

composer require mprince/laravel7-pointable

然后在 app/config/app.php 中包含服务提供者。

'providers' => [
    Mprince\Pointable\PointableServiceProvider::class
];

最后需要发布。

php artisan vendor:publish --provider="Mprince\Pointable\PointableServiceProvider"

然后运行迁移。

php artisan migrate

设置模型

<?php

namespace App;

use Mprince\Pointable\Contracts\Pointable;
use Mprince\Pointable\Traits\Pointable as PointableTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model implements Pointable
{
    use PointableTrait;
}

添加积分

$user = User::first();
$amount = 10; // (Double) Can be a negative value
$message = "The reason for this transaction";

//Optional (if you modify the point_transaction table)
$data = [
    'ref_id' => 'someReferId',
];

$transaction = $user->addPoints($amount,$message,$data);

dd($transaction);

扣除积分

$user = User::first();
$amount = 10; // (Double) Can be a negative value
$message = "The reason for this transaction";

//Optional (if you modify the point_transaction table)
$data = [
    'ref_id' => 'someReferId',
];

$transaction = $user->subPoints($amount,$message,$data);

dd($transaction);

获取当前积分

$user = User::first();
$points = $user->currentPoints();

dd($points);

获取交易

$user = User::first();
$user->transactions;

//OR
$user['transactions'] = $user->transactions(2)->get(); //Get last 2 transactions

dd($user);

计算交易数量

$user = User::first();
$user['transactions_total'] = $user->countTransactions();

dd($user);