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, 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Laravel Variable in JQuery?
- Laravel Cashier Stripe Subscription Example Tutorial
- Laravel Redirect to Route from Controller Example
- How to Use Google Translator in Laravel?
- How to Convert Collection to JSON in Laravel?
- Laravel Ajax GET Request Example Tutorial
- How to Get Environment Variable in Laravel React JS?
- Laravel 9 Custom Email Verification Tutorial
- Laravel Contact Form Send Email Tutorial
- How to Use Factory in Seeder Laravel?
- How to Add Country List in Laravel?
- Laravel Livewire Toastr Notifications Example
- Laravel Create JSON File & Download From Text Example