Home Laravel How To Make Money/Currency Format In Laravel ?

How To Make Money/Currency Format In Laravel ?

by Shahriar Sagor

Hello Dev’s today now in this article i will provide you some of the most important example of laravel convert number to money format. So if you want to know about an example of laravel currency format example then i hope you are in the right place. From here you will be learn laravel blade directive for currency format. So here i will step by step explain the money format in laravel example. We will do the following things for the currency format in laravel with examples.

We can also use this example with any laravel 6, laravel 7, laravel 8 and laravel 9 version.

If you want to convert the number into currency format with the comma or dot as like 13000 into 13,000.00, 160000 into 1,60,000.00 etc., Then, here i will give two very simple example of convert number into the money format in laravel application.

Now In the first example, i will be create custom blade directive for the money format, So we can use the @money(12000) in our blade file.

And then in the second example, i will be create controller function and then use it in the method.

So, let’s start and see the below code example.

Example 1:

Now in this example, i will be create custom blade directive in AppServiceProvider service provide file. Then i will create the @money() directive for the convert number into money format. So we can see the 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>

Example 2:

Now in this example, i will be create DemoController with moneyFormat() method for convert the number into currency format. So we can see this in the below code:

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);
    }
}

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like