Laravel Currency Format Example Tutorial

Sep 14, 2022 . Admin



Hello Friends,

This tutorial shows you laravel currency format example tutorial. step by step explain money format in laravel example. This article goes in detailed on laravel blade directive for currency format. This post will give you simple example of currency format in laravel with examples. You just need to some step to done laravel convert number 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