Learn to build an interactive EMI calculator in Laravel! This [2025] step-by-step guide provides full code using Bootstrap, JavaScript, Chart.js, and includes an amortization schedule.
Add a powerful EMI calculator to your Laravel website. Follow our detailed [2025] tutorial with code snippets for interactive UI, real-time calculations, charts, and payment schedules.
Core Features:
Inputs: Loan Amount, Annual Interest Rate, Loan Tenure (in years).
Real-time Calculation: Updates EMI, Total Interest, and Total Payable instantly as inputs change.
Visual Representation: A pie chart showing the principal vs. total interest breakdown.
Amortization Schedule: A detailed table showing the payment breakdown (principal, interest, remaining balance) for each month.
Responsive Design: Works well on desktop, tablets, and mobile devices.
Step 1: Create the Controller
Run this command in your Laravel project's terminal:
Open app/Http/Controllers/EmiCalculatorController.php and add the following method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EmiCalculatorController extends Controller
{
/**
* Display the EMI calculator view.
*
* @return \Illuminate\View\View
*/
public function index()
{
return view('emi_calculator'); // We will create this view next
}
}
Step 2: Define the Route
Open routes/web.php and add the following route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmiCalculatorController; // Make sure to import the controller
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
// Add this line for the EMI Calculator
Route::get('/emi-calculator', [EmiCalculatorController::class, 'index'])->name('emi.calculator');
Step 3: Create the Blade View (emi_calculator.blade.php)
Create a new file at resources/views/emi_calculator.blade.php. Paste the following code into it. This includes HTML structure, Bootstrap classes, input fields, result displays, the chart canvas, and the amortization table structure.
Make sure EmiCalculatorController.php is in the app/Http/Controllers/ directory.
Make sure emi_calculator.blade.php is in the resources/views/ directory.
Make sure the route is added to routes/web.php.
Access the Calculator:
Start your Laravel development server: php artisan serve
Open your browser and navigate to http://127.0.0.1:8000/emi-calculator (or whatever URL your server uses + /emi-calculator).
How it Works:
Laravel Backend: The Laravel part is minimal. The route directs the request /emi-calculator to the index method of EmiCalculatorController. This controller simply loads and returns the emi_calculator.blade.php view.
Blade View (HTML & Structure): This file sets up the visual structure using standard HTML and Bootstrap 5 classes for layout, forms, tables, and styling. It includes:
Input fields (number type for direct entry, range type for sliders) for loan amount, interest rate, and tenure.
Placeholders (div elements with IDs like emiResult) to display the calculated results.
A <canvas> element for Chart.js to draw the pie chart.
A <table> structure for the amortization schedule.
CSS: Basic inline CSS is provided for better presentation (<style> block). Bootstrap handles most of the responsiveness and basic styling.
JavaScript (Client-Side Logic): This is where the magic happens:
DOM Element Selection: Gets references to all the input fields, sliders, result display areas, table body, and chart canvas.
Event Listeners: Attaches input event listeners to all input fields and sliders. Whenever the value changes in any of them, the calculateAndDisplayEMI function is triggered.
Slider Synchronization: Code ensures that changing the slider updates the number input and vice-versa. It also updates the text displaying the slider's current value.
calculateAndDisplayEMI(): This is the core function. It reads the current values from the inputs, validates them, performs the EMI calculation using the standard formula EMI = P × r × (1 + r)ⁿ / ((1 + r)ⁿ - 1) (where r is the monthly interest rate and n is the number of months), calculates total interest and total amount, and updates the result display elements using textContent. It formats the currency using Intl.NumberFormat for better readability. It then calls generateAmortizationSchedule and updateChart.
generateAmortizationSchedule(): Clears the previous table content. It then loops through each month of the loan tenure, calculates the interest and principal portion for that month's EMI payment, and determines the remaining balance. It creates a table row (<tr>) for each month and appends it to the table body (#amortizationTableBody). It includes logic to handle potential floating-point inaccuracies in the final payment.
updateChart(): Uses the Chart.js library. It takes the principal and total interest as input. If a chart instance already exists, it updates its data; otherwise, it creates a new Pie chart on the <canvas> element. Tooltips are formatted to show currency.
Initial Load: When the page finishes loading (DOMContentLoaded), it calls updateSliderDisplays and calculateAndDisplayEMI once to show results based on the default values set in the HTML.
Libraries:
Bootstrap 5: Included via CDN for rapid UI development and responsiveness.
Chart.js: Included via CDN to create the interactive pie chart.
Customization:
Currency: Change 'en-IN', 'INR', and the ₹ symbol in the JavaScript (currencyFormatter) and HTML (#currencySymbol) to match your target currency (e.g., 'en-US', 'USD', $).
Slider Ranges: Adjust the min, max, and step attributes on the <input type="range"> and corresponding <input type="number"> elements if needed.
Styling: Modify the inline CSS or add your own CSS file (linked in the <head>) for further visual customization. If you're using Laravel Mix or Vite, compile your assets instead of using CDNs for better performance and management.
Defaults: Change the value="..." attributes in the HTML input fields to set different default values when the page loads.
This setup provides a fully functional, interactive, and visually appealing EMI calculator integrated directly into your Laravel application.
Change Currency:
Okay, changing the currency in the EMI calculator involves modifications in two places within the resources/views/emi_calculator.blade.php file:
The displayed currency symbol in the HTML.
The JavaScript Intl.NumberFormatobject which handles the formatting of calculated values.
Here’s how to do it, step-by-step:
Step 1: Change the HTML Currency Symbol
Open the resources/views/emi_calculator.blade.php file.
Locate the "Loan Amount" label section (around line 60 in the provided code).
Find the <span> element with the ID currencySymbol:
Scroll down to the <script> section at the bottom of the emi_calculator.blade.php file.
Find the line where the currencyFormatter constant is defined (around line 117):
const currencyFormatter = new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', minimumFractionDigits: 2 }); // Adjust 'en-IN' and 'INR' as needed
You need to change two parts here:
Locale String ('en-IN'): This determines the formatting conventions (like using commas or periods for separators). Replace 'en-IN' with the appropriate locale code for your target currency/region.
Currency Code ('INR'): This must be the standard ISO 4217 currency code for your desired currency. Replace 'INR' with the correct code.
Here are examples for different currencies:
For Bangladeshi Taka (BDT - ৳): Use 'bn-BD' (Bengali locale for Bangladesh) or 'en-BD' (English locale for Bangladesh) and 'BDT'.
// Using Bengali locale
const currencyFormatter = new Intl.NumberFormat('bn-BD', { style: 'currency', currency: 'BDT', minimumFractionDigits: 2 });
// Or using English locale (might format slightly differently)
// const currencyFormatter = new Intl.NumberFormat('en-BD', { style: 'currency', currency: 'BDT', minimumFractionDigits: 2 });
For US Dollar (USD - $): Use 'en-US' locale and 'USD' currency code.
For Euro (EUR - €): You can use various European locales like 'de-DE' (Germany), 'fr-FR' (France), 'es-ES' (Spain), etc., with the 'EUR' currency code. The locale affects whether commas or periods are used as decimal separators.
// Example using German locale (comma decimal separator)
const currencyFormatter = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR', minimumFractionDigits: 2 });
For British Pound (GBP - £): Use 'en-GB' locale and 'GBP' currency code.
Save the emi_calculator.blade.php file after making both changes (HTML symbol and JavaScript formatter).
Refresh the EMI calculator page in your browser (http://yourdomain/emi-calculator or your specific URL).
You should now see the new currency symbol next to the "Loan Amount" label, and all the calculated results (Monthly EMI, Total Interest, Total Amount, Amortization Schedule values) should be formatted according to the new currency and locale settings.
Learn how to implement a versatile pop-up box in Laravel, designed to showcase custom images and text content on your website's frontend. A dedicated admin panel empowers administrators to easily modify the pop-up's appearance, including colors, size, content, and visibility status. This provides a code-free way for non-developers to manage website announcements or promotions directly.
This code creates a user-friendly contact form with HTML. CSS styles the form for a clean, modern aesthetic. It includes fields for name, email, subject, and message. The form is designed for easy data collection and submission.