Giter Site home page Giter Site logo

balance's Introduction

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. Laravel 11 supported

Installation

You can install the package via composer:

composer require mreduar/laravel-balance

You can publish and run the migrations with:

php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="MrEduar\Balance\BalanceServiceProvider" --tag="config"

This is the contents of the published config file:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Table name
    |--------------------------------------------------------------------------
    |
    | Table name to use to store balance history transactions.
    |
    */

    'table' => 'balance_history',

];

Usage

Adding the HasBalance trait will enable balance functionality on the Model.

use Illuminate\Foundation\Auth\User as Authenticatable;
use MrEduar\Balance\HasBalance;

class User extends Authenticatable
{
    use HasBalance;
}

Basic operations

$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.

$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.

$user->hasBalance();
$user->hasBalance(2575);

Check if model has no balance

Check if there is no more balance.

$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.

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 for details.

License

The MIT License (MIT). Please see License File for more information.

balance's People

Contributors

mreduar avatar bruhanda avatar chapdel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.