1. Go to this page and download the library: Download lyrasoft/token-coin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$mig->createTable(
User::class,
function (Schema $schema) {
// ...
$schema->decimal('token_coins')->length('20,4');
// ...
$schema->addIndex('token_coins');
// ...
}
);
// OR
$mig->updateTable(
User::class,
function (Schema $schema) {
$schema->decimal('token_coins')->length('20,4')->after('password');
$schema->addIndex('token_coins');
}
);
use \Lyrasoft\TokenCoin\Enum\TokenCoinAction;
foreach ($userIds as $userId) {
/** @var \Lyrasoft\TokenCoin\Service\TokenCoinService $tokenCoinService */
$tokenCoinService->modifyAndAddHistory(
type: 'main',
targetId: $userId,
action: TokenCoinAction::PLUS,
value: 500,
// You can modify TokenCoinHistory entity here
beforeSave: function (TokenCoinHistory $history) use ($currentUser, $tokenCoinData) {
$history->setNote($tokenCoinData['note']); // The note messgage
$history->setAgentId($currentUser->getId()); // Who did this action
},
// Udpdate remaining values
updateRemain: function (float $remain, mixed $targetId, string|\BackedEnum $type) use ($orm, $currentUser) {
$orm->updateBatch(
User::class,
[
'token_coins' => $remain,
],
['id' => $targetId]
);
}
);
}
/** @var \Lyrasoft\TokenCoin\Service\TokenCoinService $tokenCoinService */
// plus/reduce/use actions
$tokenCoinService->plus($type, 500, $targetId, ...);
$tokenCoinService->reduce($type, 500, $targetId, ...);
$tokenCoinService->use($type, 500, $targetId, ...);
$tokenCoinService->modifyAndAddHistory($type, $action, $value, $targetId, ...);
// Other utilities
// Is enough to use/reduce
$tokenCoinService->isEnoughToReduce($type, $targetId, 500); // (bool)
// Is the remain larger than 0
$tokenCoinService->hasBalance($type, $targetId); // (bool)
// Get remaining value
$tokenCoinService->getRemain($type, $targetId); // (float)
// Get laat history entity
$tokenCoinService->getLastHistory($type, $targetId, sortBy: 'id'); // ?TokenCoinHistory
// Sum the total usage of a month of given time.
$tokenCoinService->getUsageTotalByMonth($type, $targetId, current: 'now'); // (float)
// Sum the total usage between two dates
$tokenCoinService->getUsageTotalBetween($type, $targetId, $start, $end); // (float)