PHP code example of dinhdjj / laravel-transaction

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

    

dinhdjj / laravel-transaction example snippets


return [
    'table' => env('TRANSACTION_TABLE', 'transactions'),
];

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
    {
    }
}

    $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(...);
bash
php artisan vendor:publish --tag="transaction-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="transaction-config"
bash
php artisan vendor:publish --tag="transaction-views"