Laravel Money Currency Convert Example | websolutioncode.com
Laravel Money Currency Convert Example | websolutioncode.com

Laravel Money/Currency Convert Example

Laravel, a popular PHP web application framework, offers robust features for developing scalable and maintainable applications. Handling Laravel Money/Currency Convert is common requirement in many projects. Laravel provides convenient tools for managing and formatting currency, making it easy for developers to work with monetary values.

Understanding Laravel’s Money Format

Laravel’s Money Format feature is powered by the MoneyPHP library, which is a PHP implementation of the Money pattern. The Money pattern helps in dealing with monetary values in a consistent and safe manner. Laravel leverages this library to provide developers with a straightforward and efficient way to work with currencies.

Installation

Before diving into the code, ensure that your Laravel project has the MoneyPHP library installed. You can add it to your project using Composer:

composer require moneyphp/money

Once installed, Laravel automatically integrates the MoneyPHP library into its ecosystem.

Creating Money Instances

In Laravel, you can create Money instances using the Money facade. Here’s an example:

use Money\Money;

$amount = Money::USD(5000); // Creates a Money instance with 5000 USD

This creates a Money instance with the specified amount in the specified currency (USD in this case).

Formatting Money

Formatting money in a human-readable way is crucial for a good user experience. Laravel provides a convenient money_format method for this purpose:

use Money\Money;

$amount = Money::USD(5000);

$formattedAmount = $amount->format(); // Outputs "$50.00"

This method automatically formats the money amount with the appropriate currency symbol and decimal places.

Currency Conversion

Laravel also facilitates currency conversion effortlessly. You can convert money from one currency to another using the convertTo method:

use Money\Money;

$amount = Money::USD(5000);

$convertedAmount = $amount->convertTo('EUR'); // Converts to Euro

The convertTo method takes care of the exchange rate and returns the equivalent amount in the desired currency.

Practical Example: Displaying Prices in a Laravel Blade View

Let’s create a simple Laravel Blade view to display product prices in a user-friendly format.

  1. Open your Laravel project and create a new Blade view file, for example, product.blade.php.
  2. In the controller, pass the product price as a Money instance to the view:
use Money\Money;

public function showProduct()
{
    $productPrice = Money::USD(2999);
    return view('product', compact('productPrice'));
}

Now, in your Blade view, display the formatted price:

<h2>Product Price: {{ $productPrice->format() }}</h2>

This simple example demonstrates how to use Laravel’s Money format to display prices in a user-friendly manner.

Conclusion

Laravel’s Money format feature, powered by the MoneyPHP library, makes handling currency-related tasks straightforward and efficient. From creating money instances to formatting amounts and performing currency conversion, Laravel simplifies the process for developers. By integrating these tools into your Laravel project, you can ensure a seamless and user-friendly experience when dealing with monetary values.

Leave a Reply