|
| 1 | +# Laravel Balance |
| 2 | + |
| 3 | +Maintains a history of balance movements in the eloquent models. This simple package will keep track of the balance of your models. You can increase, decrease, reset and set the balance. It is also possible to check if a model has a positive balance or no balance. |
| 4 | + |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +You can install the package via composer: |
| 9 | + |
| 10 | +``` bash |
| 11 | +composer require mreduar/laravel-balance |
| 12 | +``` |
| 13 | +You can publish and run the migrations with: |
| 14 | + |
| 15 | +```bash |
| 16 | +php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="migrations" |
| 17 | +php artisan migrate |
| 18 | +``` |
| 19 | +You can publish the config file with: |
| 20 | + |
| 21 | +```bash |
| 22 | +php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="config" |
| 23 | +``` |
| 24 | +This is the contents of the published config file: |
| 25 | + |
| 26 | +``` |
| 27 | +<?php |
| 28 | +
|
| 29 | +return [ |
| 30 | +
|
| 31 | + /* |
| 32 | + |-------------------------------------------------------------------------- |
| 33 | + | Table name |
| 34 | + |-------------------------------------------------------------------------- |
| 35 | + | |
| 36 | + | Table name to use to store balance history transactions. |
| 37 | + | |
| 38 | + */ |
| 39 | +
|
| 40 | + 'table' => 'balance_history', |
| 41 | +
|
| 42 | +]; |
| 43 | +``` |
| 44 | +## Usage |
| 45 | + |
| 46 | +Adding the `HasBalance` trait will enable balance functionality on the Model. |
| 47 | + |
| 48 | +``` php |
| 49 | +use Illuminate\Foundation\Auth\User as Authenticatable; |
| 50 | +use MrEduar\Balance\HasBalance; |
| 51 | + |
| 52 | +class User extends Authenticatable |
| 53 | +{ |
| 54 | + use HasBalance; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +##### Basic operations |
| 59 | + |
| 60 | +```php |
| 61 | +$user->increaseBalance(2575); |
| 62 | +$user->balance; // 25.75 |
| 63 | + |
| 64 | +$user->decreaseBalance(2575); |
| 65 | +$user->balance; // 0 |
| 66 | + |
| 67 | +$user->modifyBalance(-2537); |
| 68 | +$user->balance; // -25.37 |
| 69 | + |
| 70 | +$user->modifyBalance(3037); |
| 71 | +$user->balance; // 5 |
| 72 | +``` |
| 73 | + |
| 74 | +##### Reset balance |
| 75 | + |
| 76 | +It's also possible to reset the balance and directly setting a new value. |
| 77 | + |
| 78 | +```php |
| 79 | +$user->resetBalance(); // 0 |
| 80 | + |
| 81 | +$user->resetBalance(10); // 10 |
| 82 | +``` |
| 83 | + |
| 84 | +##### Check if model has balance |
| 85 | + |
| 86 | +Check if there is a positive balance or a balance greater than that provided. |
| 87 | +```php |
| 88 | +$user->hasBalance(); |
| 89 | +$user->hasBalance(2575); |
| 90 | +``` |
| 91 | + |
| 92 | +##### Check if model has balance |
| 93 | + |
| 94 | +Check if there is a positive balance or a balance greater than that provided. |
| 95 | +```php |
| 96 | +$user->hasBalance(); |
| 97 | +$user->hasBalance(2575); |
| 98 | +``` |
| 99 | + |
| 100 | +##### Check if model has no balance |
| 101 | + |
| 102 | +Check if there is no more balance. |
| 103 | +```php |
| 104 | +$user->hasNoBalance(); |
| 105 | +``` |
| 106 | + |
| 107 | +## Contributing |
| 108 | + |
| 109 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 110 | + |
| 111 | + |
| 112 | +## License |
| 113 | + |
| 114 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments