Laravel Money/Currency Format Example

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 and laravel 9 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 AppProviders;

use IlluminateSupportServiceProvider;

use IlluminateSupportFacadesBlade;

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); ?>";

});

}

}

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

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 AppHttpControllers;

use IlluminateHttpRequest;

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…

1 thought on “Laravel Money/Currency Format Example”

Leave a Comment