@@ -15,8 +15,139 @@ You can install the package via composer:
1515composer require coreproc/laravel-wallet-plus
1616```
1717
18+ You can publish the migration with:
19+
20+ ``` bash
21+ php artisan vendor:publish --provider=" CoreProc\WalletPlus\WalletPlusServiceProvider" --tag=" migrations"
22+ ```
23+
24+ After the migration file has been published you can create the wallet-plus tables by running the migration:
25+
26+ ``` bash
27+ php artisan migrate
28+ ```
29+
1830## Usage
1931
32+ First, you'll need to add the ` HasWallets ` trait to your model.
33+
34+ ``` php
35+ use CoreProc\WalletPlus\Models\Traits\HasWallets;
36+ use Illuminate\Foundation\Auth\User as Authenticatable;
37+ use Illuminate\Notifications\Notifiable;
38+
39+ class User extends Authenticatable
40+ {
41+ use Notifiable, HasWallets;
42+ }
43+ ```
44+
45+ By adding the ` HasWallets ` trait, you've essentially added all the wallet relationships to the model.
46+
47+ You can start by creating a wallet for the given model.
48+
49+ ``` php
50+ $user = User::find(1);
51+
52+ $wallet = $user->wallets()->create();
53+ ```
54+
55+ You can then increment the wallet balance by:
56+
57+ ``` php
58+ $wallet->incrementBalance(100);
59+ ```
60+
61+ Or decrement the balance by:
62+
63+ ``` php
64+ $wallet->decrementBalance(100);
65+ ```
66+
67+ To get the balance of the wallet, you can use the ` balance ` accessor:
68+
69+ ``` php
70+ $wallet->balance;
71+ ```
72+
73+ A wallet can be accessed using the ` wallet() ` method in the model:
74+
75+ ``` php
76+ $user->wallet();
77+ ```
78+
79+ You can set up multiple types of wallets by defining a ` WalletType ` . Simply create a wallet type entry in the
80+ ` wallet_types ` table and create a wallet using this wallet type.
81+
82+ ``` php
83+ use CoreProc\WalletPlus\Models\WalletType;
84+
85+ $walletType = WalletType::create([
86+ 'name' => 'Peso Wallet',
87+ 'decimals' => 2, // Set how many decimal points your wallet accepts here. Defaults to 0.
88+ ]);
89+
90+ $user->wallets()->create(['wallet_type_id' => $walletType->id]);
91+ ```
92+
93+ You can access a model's particular wallet type by using the ` wallet() ` method as well:
94+
95+ ``` php
96+ $pesoWallet = $user->wallet('Peso Wallet'); // This method also accepts the ID of the wallet type as a parameter
97+
98+ $pesoWallet->incrementBalance(100);
99+
100+ $pesoWallet->balance; // Returns the updated balance without having to refresh the model.
101+ ```
102+
103+ All movements made in the wallet are recorded in the ` wallet_ledgers ` table.
104+
105+ ### Defining Transactions
106+
107+ Ideally, we want to record all transactions concerning the wallet by linking it to a transaction model. Let's say we
108+ have a ` PurchaseTransaction ` model which holds the data of a purchase the user makes in our app.
109+
110+ ``` php
111+ use Illuminate\Database\Eloquent\Model;
112+
113+ class PurchaseTransaction extends Model
114+ {
115+ //
116+ }
117+ ```
118+
119+ We can link this ` PurchaseTransaction ` to the wallet ledger by implementing the ` WalletTransaction ` contract to this
120+ model and using this transaction to decrement (or increment, whatever the case may be) the wallet balance.
121+
122+ ``` php
123+ use CoreProc\WalletPlus\Contracts\WalletTransaction;
124+ use Illuminate\Database\Eloquent\Model;
125+
126+ class PurchaseTransaction extends Model implements WalletTransaction
127+ {
128+ public function getAmount()
129+ {
130+ return $this->amount;
131+ }
132+ }
133+ ```
134+
135+ Now we can use this in the wallet:
136+
137+ ``` php
138+ $wallet = $user->wallet('Peso Wallet');
139+
140+ $purchaseTransaction = PurchaseTransaction::create([
141+ ...,
142+ 'amount' => 100,
143+ ]);
144+
145+ $wallet->decrementBalance($purchaseTransaction);
146+ ```
147+
148+ By doing this, you will be able to see in the ` wallet_ledgers ` table the transaction that is related to the movement
149+ in the wallet.
150+
20151### Testing
21152
22153``` bash
@@ -35,8 +166,6 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
35166
36167If you discover any security related issues, please email
[email protected] instead of using the issue tracker.
37168
38-
39-
40169## Credits
41170
42171- [ Chris Bautista] ( https://github.com/chrisbjr )
0 commit comments