{"id":27145442,"url":"https://github.com/elegantengineeringtech/laravel-money","last_synced_at":"2025-04-08T09:48:15.775Z","repository":{"id":63131208,"uuid":"565433361","full_name":"ElegantEngineeringTech/laravel-money","owner":"ElegantEngineeringTech","description":"Brick/Money for Laravel. Parse, Cast and Validate.","archived":false,"fork":false,"pushed_at":"2025-03-27T17:12:07.000Z","size":115,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T17:30:01.295Z","etag":null,"topics":["larave-package","laravel","money"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ElegantEngineeringTech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ElegantEngineeringTech"}},"created_at":"2022-11-13T11:59:00.000Z","updated_at":"2025-03-27T17:18:54.000Z","dependencies_parsed_at":"2024-04-15T17:46:14.505Z","dependency_job_id":"ea436654-7692-44a7-9c56-20f3b6890f85","html_url":"https://github.com/ElegantEngineeringTech/laravel-money","commit_stats":{"total_commits":28,"total_committers":5,"mean_commits":5.6,"dds":0.5714285714285714,"last_synced_commit":"b862b11275a745b441ff37b49eda94b7c298e06f"},"previous_names":["finller/laravel-money"],"tags_count":15,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-money","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-money/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-money/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-money/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElegantEngineeringTech","download_url":"https://codeload.github.com/ElegantEngineeringTech/laravel-money/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247818607,"owners_count":21001383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["larave-package","laravel","money"],"created_at":"2025-04-08T09:48:15.115Z","updated_at":"2025-04-08T09:48:15.768Z","avatar_url":"https://github.com/ElegantEngineeringTech.png","language":"PHP","funding_links":["https://github.com/sponsors/ElegantEngineeringTech"],"categories":[],"sub_categories":[],"readme":"# Elegant Integration of Brick/Money for Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/elegantly/laravel-money.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-money)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-money/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-money/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-money/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-money/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/elegantly/laravel-money.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-money)\n\n## Table of Contents\n\n-   [Introduction](#introduction)\n-   [Features](#features)\n-   [Installation](#installation)\n-   [Configuration](#configuration)\n-   [Storing Money in the Database](#storing-money-in-the-database)\n-   [Usage](#usage)\n    -   [Casting with a Column as Currency (Recommended)](#casting-with-a-column-as-currency-recommended)\n    -   [Casting with a Defined Currency](#casting-with-a-defined-currency)\n    -   [Parsing Values to Money Instances](#parsing-values-to-money-instances)\n    -   [Validation Rule](#validation-rule)\n-   [Testing](#testing)\n-   [Changelog](#changelog)\n-   [Contributing](#contributing)\n-   [Security](#security)\n-   [Credits](#credits)\n-   [License](#license)\n\n## Introduction\n\nThis 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.\n\n## Features\n\n-   **MoneyCast**: Cast model attributes to `Brick\\Money\\Money`.\n-   **MoneyParse**: Convert various data types into `Brick\\Money\\Money` instances.\n-   **ValidMoney**: Implement money validation rules.\n\n## Installation\n\nInstall the package via Composer:\n\n```bash\ncomposer require elegantly/laravel-money\n```\n\n## Configuration\n\nTo customize the default settings, publish the configuration file:\n\n```bash\nphp artisan vendor:publish --tag=\"money-config\"\n```\n\nThe default configuration file (`config/money.php`) contains:\n\n```php\nreturn [\n    'default_currency' =\u003e 'USD',\n];\n```\n\n## Storing Money in the Database\n\nThe 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.\n\nExample migration:\n\n```php\nSchema::create('invoices', function (Blueprint $table) {\n    $table-\u003eid();\n    $table-\u003ebigInteger('amount'); // Store in cents\n    $table-\u003estring('currency', 3); // ISO currency code\n    $table-\u003etimestamps();\n});\n```\n\n## Usage\n\n### Casting with a Column as Currency (Recommended)\n\nIf your database stores both the amount and the currency in separate columns, you can specify the currency column like this:\n\n```php\nuse Elegantly\\Money\\MoneyCast;\n\n/**\n * @property ?Money $amount\n * @property ?string $currency\n **/\nclass Invoice extends Model {\n    /**\n     * @return array\u003cstring, string\u003e\n     */\n    protected function casts(): array\n    {\n        return [\n            'amount' =\u003e MoneyCast::class . ':currency'\n        ];\n    }\n}\n```\n\n### Casting with a Defined Currency\n\nYou can also define a specific currency for money casting instead of referencing a column:\n\n```php\nuse Elegantly\\Money\\MoneyCast;\n\n/**\n * @property ?Money $price\n * @property ?Money $cost\n **/\nclass Invoice extends Model {\n     /**\n     * @return array\u003cstring, string\u003e\n     */\n    protected function casts(): array\n    {\n        return [\n            'cost' =\u003e MoneyCast::class . ':EUR',\n            'price' =\u003e MoneyCast::class . ':USD'\n        ];\n    }\n}\n```\n\n### Parsing Values to Money Instances\n\nConvert strings, integers, or floats into `Brick\\Money\\Money` instances using `MoneyParser`:\n\n```php\nuse Elegantly\\Money\\MoneyParser;\n\nMoneyParser::parse(null, 'EUR'); // null\nMoneyParser::parse(110, 'EUR'); // 110.00€\nMoneyParser::parse(100.10, 'EUR'); // 100.10€\nMoneyParser::parse('', 'EUR'); // null\nMoneyParser::parse('1', 'EUR'); // 1.00€\nMoneyParser::parse('100.10', 'EUR'); // 100.10€\n```\n\n### Validation Rule\n\n#### Using `ValidMoney` in Livewire\n\n```php\nnamespace App\\Livewire;\n\nuse Elegantly\\Money\\Rules\\ValidMoney;\nuse Illuminate\\Foundation\\Http\\FormRequest;\n\nclass CustomComponent extends Component\n{\n    #[Validate([\n        new ValidMoney(nullable: false, min: 0, max: 100)\n    ])]\n    public ?int $price = null;\n}\n```\n\n#### Using `ValidMoney` in Form Requests\n\n```php\nnamespace App\\Http\\Requests;\n\nuse Elegantly\\Money\\Rules\\ValidMoney;\nuse Illuminate\\Foundation\\Http\\FormRequest;\n\nclass CustomFormRequest extends FormRequest\n{\n    public function rules()\n    {\n        return [\n            'price' =\u003e [\n                new ValidMoney(\n                    nullable: false,\n                    min: 0,\n                    max: 100\n                )\n            ],\n        ];\n    }\n}\n```\n\n## Testing\n\nRun the package tests with:\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nRefer to the [CHANGELOG](CHANGELOG.md) for details on recent updates and modifications.\n\n## Contributing\n\nContributions are welcome! See [CONTRIBUTING](CONTRIBUTING.md) for guidelines.\n\n## Security\n\nIf you discover any security vulnerabilities, please review our [security policy](../../security/policy) to report them responsibly.\n\n## Credits\n\n-   [Quentin Gabriele](https://github.com/QuentinGab)\n-   [All Contributors](../../contributors)\n\n## License\n\nThis package is released under the MIT License. See [LICENSE.md](LICENSE.md) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felegantengineeringtech%2Flaravel-money","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felegantengineeringtech%2Flaravel-money","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felegantengineeringtech%2Flaravel-money/lists"}