Laravel Money Format Example Tutorial

Sep 14, 2022

.

Admin

Laravel Money Format Example Tutorial

Hello Friends,

This example is focused on laravel money format example tutorial. if you have a question about laravel currency format example then I will give a simple example with a solution. you will learn laravel blade directive for currency format. This article goes in detailed on laravel converting numbers to money format.

If you need to convert number into currency format with comma or dot-like 12000 into 12,000.00, 120000 into 1,20,000.00 etc., Then, I will give you an example of converting numbers into money format in laravel application.

In this example, we will create a controller function and use it in the method.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel


This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: create CurrencyFormatController

we will create CurrencyFormatController with moneyFormat() method to convert number into currency format. so you can see the below code with output:

php artisan make:controller CurrencyFormatController

App\Http\Controllers\CurrencyFormatController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CurrencyFormatController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$amount = $this->moneyFormat(12000);

print($amount);

}

/**

* Write code on Method

*

* @return response()

*/

public function moneyFormat($amount)

{

return '$' . number_format($amount, 2);

}

}

Step 3: Create Format Route

web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\CurrencyFormatController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('format',[CurrencyFormatController::class,'index']);

Step 4: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

http://localhost:8000/format

Output:

$12,000.00

#Laravel