dinhdjj / laravel-transaction
Give laravel model ability transfer/receive balance
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2023-10-11 06:17:32 UTC
README
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
Installation
You can install the package via composer:
composer require dinhdjj/laravel-transaction
You can publish and run the migrations with:
php artisan vendor:publish --tag="transaction-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="transaction-config"
This is the contents of the published config file:
return [ 'table' => env('TRANSACTION_TABLE', 'transactions'), ];
Optionally, you can publish the views using
php artisan vendor:publish --tag="transaction-views"
Usage
In your model you want give it ability transfer/receive balance
Just uses Balancable
trait implements Balancable
interface
namespace App\Models\User; use Dinhdjj\Transaction\Interfaces\Balancable as InterfacesBalancable; use Dinhdjj\Transaction\Models\Transaction; use Dinhdjj\Transaction\Traits\Balancable; class User extends Model implements InterfaceBalancable { use Balancable; protected function onReceiveBalance(Transaction $transaction): void { } protected function onTransferBalance(Transaction $transaction): void { } }
To transfer/receiver
$user1 = User::find(1); $user2 = User::find(2); // If you don't check don't worry it will throws exception if($user1->canTransferBalance(200), $user2->canReceiveBalance(200)) $transaction = $user1->transferBalance($user2, 200, 'message'); $user1->balance // get current balance $user1->transferredBalance $user1->receivedBalance // get all transferred transactions // It used standard morph many relationship $user1->transferredTransactions $user1->receivedTransactions()->get() // Other $user1->receiveBalanceFromAnonymous(...); // bypass the checking $user1->forceReceiveBalanceFromAnonymous(...); $user1->transferBalanceToAnonymous(...); $user1->forceTransferBalanceToAnonymous(...);
If you want use a part, BalanceReceivable
, BalanceTransferable
, HasTransferredTransactions
, HasReceivedTransactions
both traits and interfaces will help you
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.