1. Go to this page and download the library: Download joseftraxler/laravel-money library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
joseftraxler / laravel-money example snippets
return [
'default_currency' => 'USD', // default currency used by casts when no currency is provided
'default_precision' => null, // null = automatically detected
'currencies' => [
'JPY' => ['precision' => 0],
'BHD' => ['precision' => 3],
],
];
use JosefTraxler\LaravelMoney\Currency;
use JosefTraxler\LaravelMoney\Money;
new Money('123.45', 'EUR');
Money::fromDecimal('123.45', 'EUR');
Money::EUR('123.45');
Money::fromDecimal('123.45', Currency::of('EUR'));
enum MyCurrency: string implements JosefTraxler\LaravelMoney\Contracts\Currencyable
{
case EUR = 'EUR';
case USD = 'USD';
public function getCode(): string
{
return $this->value;
}
}
Money::fromDecimal('123.45', MyCurrency::EUR);
class Currency extends Model implements JosefTraxler\LaravelMoney\Contracts\Currencyable
{
public function getCode(): string
{
return $this->code;
}
}
$currency = new Currency(['code' => 'EUR']);
Money::fromDecimal('123.45', $currency);
use Illuminate\Database\Eloquent\Model;
use JosefTraxler\LaravelMoney\Money;
class Product extends Model
{
protected function casts(): array
{
return [
'price' => Money::class . ':EUR',
];
}
}
use Illuminate\Database\Eloquent\Model;
use JosefTraxler\LaravelMoney\Money;
class Order extends Model
{
protected function casts(): array
{
return [
'price' => Money::class . ':currency',
'currency' => 'string',
];
}
}