Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mreduar/laravel-balance
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.
https://github.com/mreduar/laravel-balance
balance hacktoberfest laravel laravel-framework laravel-package
Last synced: 1 day ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/mreduar/laravel-balance
- Owner: mreduar
- License: mit
- Created: 2021-03-19T22:44:31.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-10-10T17:08:14.000Z (12 months ago)
- Last Synced: 2024-09-16T02:25:11.747Z (18 days ago)
- Topics: balance, hacktoberfest, laravel, laravel-framework, laravel-package
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 9
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Balance
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.
## Installation
You can install the package via composer:
``` bash
composer require mreduar/laravel-balance
```
You can publish and run the migrations with:```bash
php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="migrations"
php artisan migrate
```
You can publish the config file with:```bash
php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="config"
```
This is the contents of the published config file:```
'balance_history',];
```
## UsageAdding the `HasBalance` trait will enable balance functionality on the Model.
``` php
use Illuminate\Foundation\Auth\User as Authenticatable;
use MrEduar\Balance\HasBalance;class User extends Authenticatable
{
use HasBalance;
}
```#### Basic operations
```php
$user->increaseBalance(2575);
$user->balance; // 25.75$user->decreaseBalance(2575);
$user->balance; // 0$user->modifyBalance(-2537);
$user->balance; // -25.37$user->modifyBalance(3037);
$user->balance; // 5
```#### Reset balance
It's also possible to reset the balance and directly setting a new value.
```php
$user->resetBalance(); // 0$user->resetBalance(10); // 10
```#### Check if model has balance
Check if there is a positive balance or a balance greater than that provided.
```php
$user->hasBalance();
$user->hasBalance(2575);
```#### Check if model has no balance
Check if there is no more balance.
```php
$user->hasNoBalance();
```#### Add reference and description to history
It is possible to add a reference to any of the above methods by passing an array as a second parameter with the data of the referenced model and a description.
```php
use App\Models\Podcast;$podcast = Podcast::find(1);
$user->decreaseBalance(2575, [
'description' => 'Purchase of a podcast.',
'reference' => $podcast
]);$user->increaseBalance(2575, [
'description' => 'Paypal Deposit.'
]);
```## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.