open-nebel/ewallet

Laravel Package to Easy work with virtual wallet

dev-main 2024-01-10 14:31 UTC

This package is auto-updated.

Last update: 2024-06-10 15:32:39 UTC


README

Social Card of Laravel E-wallet

Manage Wallet and More with Eloquent Models

Installation

You can install the package via composer:

composer require open-nebel/ewallet

You can publish and run the migrations with:

php artisan vendor:publish "OpenNebel\EWallet\Providers\EWalletServiceProvider"  
php artisan migrate

Usage

Prepare User Model

  • app/Models/User.php
use OpenNebel\EWallet\Interfaces\Wallet;
use OpenNebel\EWallet\Traits\HasWallet;

class User extends Authenticatable implements Wallet
{
    use HasWallet;
}
  • config/wallet.php
<?php

return [
    "owner_model" => "App\Models\User",
];

Manage Wallets

    $user = User::find(1);
    
    // Add Wallet with default name and default currency
    $user->addWallet();
    
    // or Add Wallet with name and currency
    $user->addWallet(name: "Text", currency: $currency->id);
    
    // Or Add Wallet with balance
    $user->addWallet(balance: 1000);

    $user->getWallets();
    // RESULTAT
    [
        {
            "id": 1,
            "name": "US Dollar Wallet",
            "owner_type": "App\\Models\\User",
            "owner_id": 1,
            "balance": "0.00",
            "currency_id": 7,
            "created_at": "2024-01-09T09:38:25.000000Z",
            "updated_at": "2024-01-09T09:38:25.000000Z"
        },
        {
            "id": 2,
            "name": "Text",
            "owner_type": "App\\Models\\User",
            "owner_id": 1,
            "balance": "0.00",
            "currency_id": 10,
            "created_at": "2024-01-09T09:56:43.000000Z",
            "updated_at": "2024-01-09T09:56:43.000000Z"
        }
    ]

    // Retrieve a specific wallet
    $wallet = Wallet::find(1);
    
    $user->getWallet($wallet); // by instance
    
    $user->getWallet(1); // by ID
    
    $user->getWallet("US Dollar Wallet"); // by name

    // RESULT
    {
        "id": 1,
        "name": "US Dollar Wallet",
        "owner_type": "App\\Models\\User",
        "owner_id": 1,
        "balance": "0.00",
        "currency_id": 7,
        "created_at": "2024-01-09T09:38:25.000000Z",
        "updated_at": "2024-01-09T09:38:25.000000Z"
    }