ItSolutionStuff.com

Laravel Money/Currency Format Example

By Hardik Savani • April 16, 2024
Laravel

Hi Artisan,

This article will provide some of the most important example laravel convert number to money format. if you want to see an example of laravel currency format example then you are in the right place. you will learn laravel blade directive for currency format. step by step explain money format in laravel example. you will do the following things for currency format in laravel with examples.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

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 two example of convert number into money format in laravel application.

In the first example, we will create custom blade directive for money format, so you can use @money(12000) in your blade file.

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

So, Without any further ado, let's see below code example.

Example 1:

In second example, we will create custom blade directive in AppServiceProvider service provide file. we will create @money() directive for convert number into money format. so you can see below code with output:

app/Provides/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider

{

/**

* Register any application services.

*

* @return void

*/

public function register()

{

}

/**

* Bootstrap any application services.

*

* @return void

*/

public function boot()

{

Blade::directive('money', function ($amount) {

return "<?php echo '$' . number_format($amount, 2); ?>";

});

}

}

Use in Blade File:

<p>@money(1200)</p>

Output:

$12,000.00

Example 2:

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

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController 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);

}

}

Output:

$12,000.00

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Laravel Variable in JQuery?

Read Now →

Laravel Cashier Stripe Subscription Example Tutorial

Read Now →

Laravel Redirect to Route from Controller Example

Read Now →

How to Use Google Translator in Laravel?

Read Now →

How to Convert Collection to JSON in Laravel?

Read Now →

Laravel Ajax GET Request Example Tutorial

Read Now →

How to Get Environment Variable in Laravel React JS?

Read Now →

Laravel 9 Custom Email Verification Tutorial

Read Now →

Laravel Contact Form Send Email Tutorial

Read Now →

How to Use Factory in Seeder Laravel?

Read Now →

How to Add Country List in Laravel?

Read Now →

Laravel Livewire Toastr Notifications Example

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →