1. Go to this page and download the library: Download andriichuk/laracash 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/ */
andriichuk / laracash example snippets
[
'currency' => 'USD',
'locale' => 'en_US',
]
namespace App;
use Andriichuk\Laracash\Casts\MoneyCast;
use Illuminate\Database\Eloquent\Model;
use Money\Money;
/**
* Class OrderItem
*
* @property Money $price
* @property Money $discount
*/
class OrderItem extends Model
{
protected $fillable = ['name', 'price', 'discount'];
protected $casts = [
'price' => MoneyCast::class,
'discount' => MoneyCast::class,
];
}
use App\Product;
use Illuminate\Support\Facades\Route;
Route::view('/', 'productForm', ['product' => Product::find(1)]);
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::post('products/{product}', function (Product $product, Request $request) {
$product->price = parseMoneyDecimal($request->get('price')); // 55.99 => Money::USD(5599)
});
use App\Product;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Class ProductResource
*
* @mixin Product
*/
final class ProductResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'price_as_currency' => $this->price_as_currency, // or formatMoneyAsCurrency($this->price)
];
}
}
use App\Product;
use App\Http\Resources\ProductResource;
Route::get('products/{product}', function (Product $product) {
return new ProductResource($product);
});
use App\Product;
Product::create([
'name' => 'The First Product',
'price' => 100,
]);
use App\Product;
use Money\Money;
Product::create([
'name' => 'The Second Product',
'price' => Money::USD(100),
]);
use Andriichuk\Laracash\Facades\Laracash;
use App\Product;
Product::create([
'name' => 'The Third Product',
'price' => Laracash::factory()->make(100)
]);
use App\Product;
Product::create([
'name' => 'The Fourth Product',
'price' => makeMoney(100)
]);
use Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
// Using Facade
Laracash::factory()->makeBitcoin(1000000000);
// Using helper
makeBitcoin(1000000000);
// Using native library factory call
Money::XBT(1000000000);
use App\Product;
$product = Product::find(1);
dd($product->price);
use Andriichuk\Laracash\Facades\Laracash;
use App\Product;
$product = Product::find(1);
$product->price = $product->price->add(Laracash::factory()->make(2000));
$product->save();
use \Andriichuk\Laracash\Facades\Laracash;
Laracash::factory()->make(1000);
Laracash::factory()->make('10000000000000');
use \Andriichuk\Laracash\Facades\Laracash;
use \Money\Currency;
Laracash::factory()->make(1000, 'USD');
Laracash::factory()->make(1000, new Currency('USD'));
// Or use native method Money::USD(100)
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
Laracash::formatter()->formatAsDecimal(Money::USD(100)); // "1.00"
formatMoneyAsDecimal(Money::USD(100)); // "1.00"
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
Laracash::formatter()->formatAsIntlDecimal(Money::USD(100)); // "1"
Laracash::formatter()->formatAsIntlDecimal(Money::USD(100), 'uk_UA'); // "1"
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
Laracash::formatter()->formatAsIntlCurrency(Money::USD(100)); // "$1.00"
Laracash::formatter()->formatAsIntlCurrency(Money::USD(100), 'uk_UA'); // "1,00 USD"
formatMoneyAsCurrency(Money::USD(100)); // "$1.00"
formatMoneyAsCurrency(Money::XBT(1000000000)); // "Ƀ10.00"
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
use NumberFormatter;
Laracash::formatter()->formatIntlWithStyle(Money::USD(100), 'en_US', NumberFormatter::SPELLOUT); // "one"
Laracash::formatter()->formatIntlWithStyle(Money::USD(100), 'en_US', NumberFormatter::SCIENTIFIC); // "1E0"
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
Laracash::formatter()->formatBitcoin(Money::XBT(1000000000)); // "Ƀ10.00"
// or use helper function
formatMoneyAsCurrency(makeBitcoin(1000000000)); // "Ƀ10.00"
use \Andriichuk\Laracash\Facades\Laracash;
use Money\Money;
Laracash::formatter()->formatBitcoinAsDecimal(Money::XBT(1000000000)); // "10.00000000"
// or use helper function
formatMoneyAsDecimal(makeBitcoin(1000000000)); // "10.00000000"
use Andriichuk\Laracash\Facades\Laracash;
Laracash::parser()->parseIntlCurrency('$1.00');
use Andriichuk\Laracash\Facades\Laracash;
Laracash::parser()->parseDecimal('1.30');
parseMoneyDecimal('1.30');