How to Create Rupiah Format Laravel Blade Template

Last modified date

Comments: 0

How to Create Rupiah Format on Laravel Blade Template – Brilliansolution. When displaying data, of course, we cannot be separated from the currency format, one of the solutions is to use number_format on the controller side when the data is displayed in the view. which will be displayed.

Here are the steps to add How to Create Rupiah Format Laravel Blade Template directive in Laravel:

AppServiceProvider

First open the AppServiceProviders file found in the folder

because we want to create a custom directive, we first use the facade blade in the following way:

use Illuminate\Support\Facades\Blade;

Create Custom Directive

Then after we user facades Blade in the boot method add the following line:

        Blade::directive('rupiah', function ( $expression ) { 
            return "Rp. ". number_format($expression,0,',','.');
        });

Full code on AppServiceProvider

<?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('rupiah', function ( $expression ) { 
            return "Rp. ". number_format($expression,0,',','.');
        });
    }
}

The code above serves to make the rupiah format and we named the custom directive on the blade with the rupiah name.

After we add the custom directive currency above we can call it in Laravel Blade as follows:

Call in Blade

@rupiah($data->values)

To learn details about laravel blade you can read it here.

(Visited 220 times, 1 visits today)

Leave a Reply

%d bloggers like this: