PHP Classes

Laravel Money Package: Manipulate model attribute as money values

Recommend this page to a friend!
  Info   View files Documentation   View files View files (16)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 37 This week: 1All time: 10,888 This week: 560Up
Version License PHP version Categories
laravel-money 1.0MIT/X Consortium ...5PHP 5, Libraries, Finances, Design Pa...
Description 

Author

This package can manipulate model attribute as money values.

It provides a trait that can be used in model classes of Laravel framework based applications to treat specific attributes as money amounts. Currently it can:

- Format the money attributes to display them in a given currency
- Perform money operations like withdraw an amount (subtraction) or deposit an amount (addition)
- Retrieve model class objects that match certain conditions of the values of the money attributes, like finding all objects with money attributes with given values
- Perform money amount operations with all objects of a model class with money attributes, like calculating the sum of the amounts of money of all objects of a model class

Innovation Award
PHP Programming Innovation award nominee
November 2020
Number 9
Many applications need to perform operations with money amounts like those that implement e-commerce solutions.

This package implements operations with money amounts that go behind money value formatting as text, like for instance deposit and withdraw funds or calculating sums of amounts stored in objects of model classes implemented with the Laravel framework.

This package can greatly simplify complex operations that you may need to perform with money amounts of applications related finances.

Manuel Lemos
Picture of Moamen Eltouny
  Performance   Level  
Name: Moamen Eltouny <contact>
Classes: 36 packages by
Country: Egypt Egypt
Age: 31
All time rank: 259926 in Egypt Egypt
Week rank: 52 Up1 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 20x

Documentation

<p align="center"><a href="https://pharaonic.io" target="_blank"><img src="https://raw.githubusercontent.com/Pharaonic/logos/main/money.jpg" width="470"></a></p>

<p align="center"> <a href="https://github.com/Pharaonic/laravel-money" target="_blank"><img src="http://img.shields.io/badge/source-pharaonic/laravel--money-blue.svg?style=flat-square" alt="Source"></a> <a href="https://packagist.org/packages/pharaonic/laravel-money" target="_blank"><img src="https://img.shields.io/packagist/v/pharaonic/laravel-money?style=flat-square" alt="Packagist Version"></a><br> <a href="https://laravel.com" target="_blank"><img src="https://img.shields.io/badge/Laravel->=6.0-red.svg?style=flat-square" alt="Laravel"></a> <img src="https://img.shields.io/packagist/dt/pharaonic/laravel-money?style=flat-square" alt="Packagist Downloads"> <img src="http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="Source"> </p>

Laravel Money provides a quick and easy methods to manage your model money [150+ Currencies].

Install

Install the latest version using Composer:

$ composer require pharaonic/laravel-money

then publish the migration & config files

$ php artisan vendor:publish --tag=laravel-money

Configuration & Options

Usage

<a name="config"></a>

Configuration
// config/Pharaonic/money.php

return [

    // Default Language
    'language'  => 'en',

    // Default Currency
    'currency'  => 'USD',

    // Select specific currencies
    'only'      => [],

    // Except specific currencies
    'except'    => [],

    // Default fields names
    'fields'    => ['price']
];

<a name="options"></a>

Options
use Pharaonic\Laravel\Money\Facades\Money;

// Setting default currency
Money::setCurrency('USD');

// Setting default language
// Supported language [ar, en, fr, de, es]
Money::setLanguage('ar');

// Select specific currencies 
Money::only(['USD']);
    
// Except specific currencies
Money::except(['USD']);

// Get current language
$language 		= Money::getLanguage();

// Get currency Code, Name, Symbol
$currency_code 		= Money::getCurrencyCode();
$currency_name 		= Money::getCurrencyName();
$currency_symbol 	= Money::getCurrencySymbol();

// Get currencies list (code => name)
$currencies 		= Money::getCurrenciesList();

<a name="include"></a>

How to include it in your model

...
use Pharaonic\Laravel\Helpers\Traits\HasCustomAttributes;
use Pharaonic\Laravel\Money\HasMoney;

class Person extends Model
{
    use HasCustomAttributes, HasMoney;
    
    // You can include your all monies names here.
	protected $moneyAttributes = ['balance'];
    ...
}

<a name="set"></a>

Setting Money

// Setting money to exists person
$person = Person::find(1);
$person->money('balance', 'USD', 100);

// Setting money to new person with current currency ($currency_code)
$person = Person::create([
    'balance'
    ...
]);

<a name="get"></a>

Getting & Displaying Money

$person = Person::find(1);

// Get money with specific currency
echo $person->money('balance', 'USD');

echo $person->balance; 					// 100.00
echo $person->balance->amount; 			// 100
echo $person->balance->withName(); 		// 100.00 USD
echo $person->balance->withSymbol();	// $ 100.00
echo $person->balance->toString() 		// one hundred dollars {PHP Extension intl}

<a name="actions"></a>

Money Actions

$person = Person::find(1);

$person->balance->withdraw(0.50); 	// withdraw 50 cents
$person->balance->deposit(10.50); 	// deposit 10 dollars and 50 cents
$person->balance->reset();			// resetting money to zero

<a name="relations_scopes"></a>

Relations & Scopes

// Getting monies with all currencies
$monies = $person->monies;

// Getting all People who has no monies
dd(Person::withoutMonies()->get());

// with Currency only
$pplWithMoney = Person::withMoney('USD')->get(); 
// with Currency and name
$pplWithMoney = Person::withMoney('USD', 'balance')->get(); 

// with Currencies only
$pplWithMoney = Person::withAnyMoney(['USD'])->get();
// with Currencies and names
$pplWithMoney = Person::withAnyMoney(['USD'], ['balance'])->get(); 

<a name="aggregates"></a>

Aggregates

// Getting MIN Money
echo Person::minMoney('balance');
echo Person::minMoney('balance', 'USD');

// Getting MAX Money
echo Person::maxMoney('balance');
echo Person::maxMoney('balance', 'USD');

// Getting SUM All Monies
echo Person::sumMoney('balance');
echo Person::sumMoney('balance', 'USD');

// Getting SUM Negative Monies
echo Person::sumNegativeMoney('balance');
echo Person::sumNegativeMoney('balance', 'USD');

// Getting SUM Positive Monies
echo Person::sumPositiveMoney('balance');
echo Person::sumPositiveMoney('balance', 'USD');

// Getting Average Monies
echo Person::avgMoney('balance');
echo Person::avgMoney('balance', 'USD');

// Getting Count OF Monies Rows
echo Person::countMoney();
echo Person::countMoney('balance');
echo Person::countMoney(null, 'USD');
echo Person::countMoney('balance', 'USD');

<a name="events"></a>

Events

...
class Person extends Model
{
    ...
    /
     * Setted Money with (Create/New/money method) Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function setted(string $name, string $currency, float $amount)
    {
        //
    }
    
    /
     * Withdrew Money Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function withdrew(string $name, string $currency, float $amount)
    {
        //
    }

    /
     * Deposited Money Event
     *
     * @param string $name
     * @param string $currency
     * @param float $amount
     * @return void
     */
    public function deposited(string $name, string $currency, float $amount)
    {
        //
    }

    /
     * Reset Money Event
     *
     * @param string $name
     * @param string $currency
     * @return void
     */
    public function reset(string $name, string $currency)
    {
        //
    }
    ...
}

License

MIT license


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 5 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imagedata (2 files, 1 directory)
Files folder imagedatabase (1 directory)
Files folder imageFacades (1 file)
Files folder imageModels (1 file)
  Plain text file HasMoney.php Class Class source
  Plain text file MoneyServiceProvider.php Class Class source

  Files folder image Files  /  src  /  config  
File Role Description
  Accessible without login Plain text file money.php Aux. Auxiliary script

  Files folder image Files  /  src  /  data  
File Role Description
Files folder imagelang (5 files)
  Accessible without login Plain text file and.php Aux. Auxiliary script
  Accessible without login Plain text file currencies.php Aux. Auxiliary script

  Files folder image Files  /  src  /  data  /  lang  
File Role Description
  Accessible without login Plain text file ar.php Aux. Auxiliary script
  Accessible without login Plain text file de.php Aux. Auxiliary script
  Accessible without login Plain text file en.php Aux. Auxiliary script
  Accessible without login Plain text file es.php Aux. Auxiliary script
  Accessible without login Plain text file fr.php Aux. Auxiliary script

  Files folder image Files  /  src  /  database  
File Role Description
Files folder imagemigrations (1 file)

  Files folder image Files  /  src  /  database  /  migrations  
File Role Description
  Plain text file money.stub Class Class source

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file Money.php Class Class source

  Files folder image Files  /  src  /  Models  
File Role Description
  Plain text file Money.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:37
This week:1
All time:10,888
This week:560Up