Skip to content

Commit 32760fb

Browse files
committed
doc
1 parent 8b661b6 commit 32760fb

File tree

1 file changed

+80
-36
lines changed

1 file changed

+80
-36
lines changed

README.md

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,103 @@
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-money/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-money/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
66
[![Total Downloads](https://img.shields.io/packagist/dt/elegantly/laravel-money.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-money)
77

8-
Easily use Brick/Money in your Laravel app.
8+
## Table of Contents
9+
10+
- [Introduction](#introduction)
11+
- [Features](#features)
12+
- [Installation](#installation)
13+
- [Configuration](#configuration)
14+
- [Storing Money in the Database](#storing-money-in-the-database)
15+
- [Usage](#usage)
16+
- [Casting with a Column as Currency (Recommended)](#casting-with-a-column-as-currency-recommended)
17+
- [Casting with a Defined Currency](#casting-with-a-defined-currency)
18+
- [Parsing Values to Money Instances](#parsing-values-to-money-instances)
19+
- [Validation Rule](#validation-rule)
20+
- [Testing](#testing)
21+
- [Changelog](#changelog)
22+
- [Contributing](#contributing)
23+
- [Security](#security)
24+
- [Credits](#credits)
25+
- [License](#license)
26+
27+
## Introduction
28+
29+
This package provides a seamless integration of [Brick/Money](https://github.com/brick/money) with Laravel, allowing you to handle monetary values efficiently within your application.
930

1031
## Features
1132

12-
- MoneyCast: Cast your model attributes to `Brick\Money\Money`
13-
- MoneyParse: Parse strings and other types to `Brick\Money\Money`
14-
- ValidMoney: Money validation rule
33+
- **MoneyCast**: Cast model attributes to `Brick\Money\Money`.
34+
- **MoneyParse**: Convert various data types into `Brick\Money\Money` instances.
35+
- **ValidMoney**: Implement money validation rules.
1536

1637
## Installation
1738

18-
You can install the package via Composer:
39+
Install the package via Composer:
1940

2041
```bash
2142
composer require elegantly/laravel-money
2243
```
2344

24-
You can publish the config file with:
45+
## Configuration
46+
47+
To customize the default settings, publish the configuration file:
2548

2649
```bash
2750
php artisan vendor:publish --tag="money-config"
2851
```
2952

30-
This is the content of the published config file:
53+
The default configuration file (`config/money.php`) contains:
3154

3255
```php
3356
return [
3457
'default_currency' => 'USD',
3558
];
3659
```
3760

61+
## Storing Money in the Database
62+
63+
The recommended way to store money in the database is to use a `bigInteger` column for the amount and a `string` column for the currency. This ensures precision and avoids floating-point errors. Store the amount in the smallest unit of currency (e.g., cents for USD, centimes for EUR). This approach prevents rounding issues and maintains accuracy in calculations.
64+
65+
Example migration:
66+
67+
```php
68+
Schema::create('invoices', function (Blueprint $table) {
69+
$table->id();
70+
$table->bigInteger('amount'); // Store in cents
71+
$table->string('currency', 3); // ISO currency code
72+
$table->timestamps();
73+
});
74+
```
75+
3876
## Usage
3977

40-
### Casting Using a Column as Currency (Recommended)
78+
### Casting with a Column as Currency (Recommended)
4179

42-
If you store the currency in a table column alongside the amount value, you can specify the column name like this:
80+
If your database stores both the amount and the currency in separate columns, you can specify the currency column like this:
4381

4482
```php
4583
use Elegantly\Money\MoneyCast;
4684

4785
/**
48-
* @property ?Money $price
86+
* @property ?Money $amount
4987
* @property ?string $currency
5088
**/
5189
class Invoice extends Model {
52-
53-
protected $casts = [
54-
'price' => MoneyCast::class . ':currency'
55-
];
56-
90+
/**
91+
* @return array<string, string>
92+
*/
93+
protected function casts(): array
94+
{
95+
return [
96+
'amount' => MoneyCast::class . ':currency'
97+
];
98+
}
5799
}
58100
```
59101

60-
### Casting Using a Defined Currency
102+
### Casting with a Defined Currency
61103

62-
You can cast your money to a specific currency using the currency code instead of the column name.
104+
You can also define a specific currency for money casting instead of referencing a column:
63105

64106
```php
65107
use Elegantly\Money\MoneyCast;
@@ -69,37 +111,37 @@ use Elegantly\Money\MoneyCast;
69111
* @property ?Money $cost
70112
**/
71113
class Invoice extends Model {
72-
73-
protected $casts = [
74-
'cost' => MoneyCast::class . ':EUR',
75-
'price' => MoneyCast::class . ':USD'
76-
];
77-
114+
/**
115+
* @return array<string, string>
116+
*/
117+
protected function casts(): array
118+
{
119+
return [
120+
'cost' => MoneyCast::class . ':EUR',
121+
'price' => MoneyCast::class . ':USD'
122+
];
123+
}
78124
}
79125
```
80126

81-
### Parsing a Value to a Money Instance
82-
83-
You can parse any string/int/float to a money instance using `MoneyParser`.
127+
### Parsing Values to Money Instances
84128

85-
Here are some examples of the expected behavior:
129+
Convert strings, integers, or floats into `Brick\Money\Money` instances using `MoneyParser`:
86130

87131
```php
88132
use Elegantly\Money\MoneyParser;
89133

90134
MoneyParser::parse(null, 'EUR'); // null
91-
92135
MoneyParser::parse(110, 'EUR'); // 110.00€
93136
MoneyParser::parse(100.10, 'EUR'); // 100.10€
94-
95137
MoneyParser::parse('', 'EUR'); // null
96138
MoneyParser::parse('1', 'EUR'); // 1.00€
97139
MoneyParser::parse('100.10', 'EUR'); // 100.10€
98140
```
99141

100142
### Validation Rule
101143

102-
Using `ValidMoney` within Livewire:
144+
#### Using `ValidMoney` in Livewire
103145

104146
```php
105147
namespace App\Livewire;
@@ -116,7 +158,7 @@ class CustomComponent extends Component
116158
}
117159
```
118160

119-
Using `ValidMoney` within a form request:
161+
#### Using `ValidMoney` in Form Requests
120162

121163
```php
122164
namespace App\Http\Requests;
@@ -143,21 +185,23 @@ class CustomFormRequest extends FormRequest
143185

144186
## Testing
145187

188+
Run the package tests with:
189+
146190
```bash
147191
composer test
148192
```
149193

150194
## Changelog
151195

152-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
196+
Refer to the [CHANGELOG](CHANGELOG.md) for details on recent updates and modifications.
153197

154198
## Contributing
155199

156-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
200+
Contributions are welcome! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines.
157201

158-
## Security Vulnerabilities
202+
## Security
159203

160-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
204+
If you discover any security vulnerabilities, please review our [security policy](../../security/policy) to report them responsibly.
161205

162206
## Credits
163207

@@ -166,4 +210,4 @@ Please review [our security policy](../../security/policy) on how to report secu
166210

167211
## License
168212

169-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
213+
This package is released under the MIT License. See [LICENSE.md](LICENSE.md) for details.

0 commit comments

Comments
 (0)