PHP code example of geowrgetudor / laravel-balance

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

    

geowrgetudor / laravel-balance example snippets


return [
    /**
     * Default table name.
     * If you have changed the migration, make sure you change this too.
     */
    'table' => 'balances',
    'default_currency' => 'EUR', // Default Currency
];


use Geow\Balance\Traits\HasBalance;

class User extends Model {
    // ...
    use HasBalance;

}

// Set balance (similar to increaseCredit() method - just a naming difference)
$user->setCredit(2000);

// Get balance
$user->credit;

// Increase balance
$user->increaseCredit(1000);

// Decrease balance
$user->decreaseCredit(500);

// Reset balance to 0
$user->resetCredit();

// Check if the user has balance
$user->hasCredit();

// Passing a reason for setting/increasing/deacreasing the balance
$user->setCredit(20000, 'Signup bonus');
$user->increaseCredit(1000, 'Awarded credits');
$user->decreaseCredit(250, 'Service usage');

// Get balance as currency
$user->increaseCredit(1000);
$user->credit; // returns 1000 (represeting cents)
$user->creditCurrency; // returns $10.00 (representing dollars)

// If you need to display using a different currency
$user->withCurrency('EUR')->creditCurrency // returns €10.00

// Getting all model related transactions (increases and decresed in balance)
$user->credits; // Returns \Illuminate\Database\Eloquent\Collection
bash
php artisan vendor:publish --tag="balance-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="balance-config"