mprince/laravel7-pointable

Laravel 7 的积分系统

dev-main 2022-06-16 10:55 UTC

This package is auto-updated.

Last update: 2024-09-16 15:46:35 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel Pointable

为 Laravel 7、8、9.* 提供积分交易系统。

灵感来源于 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);