PHP code example of slashdotapp / casino-balance

1. Go to this page and download the library: Download slashdotapp/casino-balance 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/ */

    

slashdotapp / casino-balance example snippets


$account = $this->accountant->getAccount($user, new \Money\Currency('EUR'));
$transaction = $this->transactionProcessor->create($account, new \Vuer\LaravelBalance\Dto\TransactionDto(100));
 php
// config/app.php
'providers' => [
    ...
    Vuer\LaravelBalance\BalanceServiceProvider::class,
];

php artisan vendor:publish

php artisan migrate
 php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Vuer\LaravelBalance\Models\AccountBalance;
use Vuer\LaravelBalance\Models\Interfaces\AccountBalanceHolderInterface

class User extends Model implements AccountBalanceHolderInterface
{
    public function accountBalances()
    {
        return $this->morphOne(AccountBalance::class, 'holder');
    }

    public function getAccount(string $currency): ?AccountBalance
    {
        return $this->accountBalances()->where('currency', $currency)->first();
    }

    public function addAccountBalance(AccountBalance $accountBalance)
    {
        $accountBalance->holder()->associate($this);
        $accountBalance->save();
    }
}
 php

use Vuer\LaravelBalance\Services\Accountant;
use Vuer\LaravelBalance\Services\TransactionProcessor;

class SomeController extends Controller
{
    /**
     * @var Accountant
     */
    private $accountant;
    
    /**
     * @var TransactionProcessor
     */
    private $transactionProcessor;

    public function __construct(Accountant $accountant, TransactionProcessor $transactionProcessor)
    {
        $this->accountant = $accountant;
        $this->transactionProcessor = $transactionProcessor;
    }
}
 php
$account = $this->accountant->getAccountOrCreate($user, new \Money\Currency('EUR'));
$account = $this->accountant->createAccount($user, new \Money\Currency('EUR'));
 php
$account = $this->accountant->getAccount($user, new \Money\Currency('EUR'));
 php
$currency = $account->getBalance()->getCurrency();
$amount = $account->getBalance()->getAmount();