open-nebel / ewallet
Laravel 包,简化虚拟钱包操作
dev-main
2024-01-10 14:31 UTC
Requires
- php: >=8.1
- illuminate/support: ^10.39
Requires (Dev)
- laravel/pint: ^1.13
- nunomaduro/collision: *
- orchestra/testbench: ^8.19
- pestphp/pest: ^2.30
- pestphp/pest-plugin-laravel: ^2.2
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-09-10 16:02:05 UTC
README
使用 Eloquent 模型管理钱包及其他功能
安装
您可以通过 composer 安装此包
composer require open-nebel/ewallet
您可以使用以下命令发布和运行迁移
php artisan vendor:publish "OpenNebel\EWallet\Providers\EWalletServiceProvider"
php artisan migrate
使用方法
准备用户模型
- 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", ];
管理钱包
$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" }