My way to handle money format input in Laravel
November 16, 2020Common usage of website is to handle data about finance, which is the most data is money and it has a format. In Indonesia for example the format is like Rp1.500.000,00. The problem is with default HTML input, it can’t show format like that by default. So how to handle it? At least here’s my may to do that in Laravel based website.
Database side
It has to handle in three side, the first in inside the database where you want to store the data. On sql there is decimal type that you can use to store number with commas. So in the migration you can simply use
1$table->decimal('amount', 8, 2);
8 is the total digits and 2 is decimal digits. Refer to the official documentation.
HTML side
So after we set database column to proper type, next we need to set the html input to prevent unwanted data that user accidentally input. In html there is
1<input type="number">
to handle number input. You can use attribute step
to define decimal digit right on the input. There’re other additional attributes that you can use to, find more here
Laravel blade side
Lastly, when you want to show the data to the user. Data format from database usually will look like this 1500000.00
and you want to show it like this 1.500.000,00
. To do this you can use php number_format()
function inside the blade.
1number_format($data, 2, ',', '.');
The first parameter is your number, next is how many decimal digit you want to show, the third is decimal separator, and the last is thousand separator. Read more in PHP manual
Those are my way to handle money data, thanks for reading this and if you want to say something leave it in discussion.
🙌🏼Share this
📋More articles
Tools that I usually used as a Web Developer
For almost 2 years I have doing web developing as a job from designing landing page until write API for laravel-based web application in windows machine. Now I like to share my everyday tools for web developing that I usually used.
You can do these on Figma to work faster
Figma became popular these days to design a web/mobile user interface. That’s because Figma gives you more modern and reliable approach when come to performance, cloud storage, and collaboration. Now I wanna share with you how you can improve your designing process to work faster with Figma.
Kindle has changed my reading habits
Reading on a physical book and reading on a kindle give different experience. Those two things feel different when open the next page, highlights the text, holding it while reading, and so on. On this post, I wanna share with you what experiences that kindle will give to me compared to physical book.
Protect your form against spam bots
I was in the middle of embedding a third-party form on the website. The form snippet is just like the usual HTML form and the website is built using Next.js. At first, I thought it’s working well, fill the fields and submit it. But my team was told that we received various weird submissions with