Laravel Money/Currency Format Example

By Hardik Savani April 16, 2024 Category : 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 :
Shares