Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

filbertumbawa15's avatar

How to Define a money Data Type in Laravel Migrations for SQL Server?

I'm working with Laravel migrations and need to define a money column type in SQL Server. However, Laravel's schema builder does not provide a direct money type.

I’ve considered two approaches:

Using raw SQL:

Schema::table('your_table', function (Blueprint $table) {
	DB::statement("ALTER TABLE your_table ADD price MONEY");
});	

Using decimal as an alternative:

Schema::table('your_table', function (Blueprint $table) {
	$table->decimal('price', 19, 4);
});

I prefer a solution that works natively with Laravel’s schema builder. Is there a way to register a custom column type for money, or is raw SQL the only option?

Any recommendations for best practices in Laravel with SQL Server are appreciated!

0 likes
10 replies
Tray2's avatar

As @jlrdw says, either store it as cents in an integer column, or as the decimal value in a decimal column.

filbertumbawa15's avatar

The problem when wanna counting of that records (the datatype field is double in SQL Server). When count the value that double datatype in Laravel. It comes not same value.

SQL Server Record A Value = 0.2 (Double datatype)

When get in Laravel Query Builder of A Value = 0.19999999 (Float in migration)

So that, i wanna change my datatype from double to money Maybe that has other configuration in Laravel to handle my case?

jlrdw's avatar

@filbertumbawa15 You will need to round off. Like in python I program metal flat pattern development and round to 6 decimals, I may have a value like:

32.47098416871

becomes 32.470984

But if

32.47098456871

becomes 32.470985

Rule do all calculations first only round final answer.

Just example. But I have had no problems using decimal in business applications, just some common sense on how to handle rounding off.

Snapey's avatar

Only ever use integers to represent money ... is a lesson learned after your first project dealing with money and taxes

3 likes
martinbean's avatar

@filbertumbawa15 Although it sounds like a good idea, even the documentation for the money data type say not to use it if you’re going to be performing calculations. From https://learn.microsoft.com/en-us/sql/t-sql/data-types/money-and-smallmoney-transact-sql?view=sql-server-ver16:

You can experience rounding errors through truncation, when storing monetary values as money and smallmoney. Avoid using this data type if your money or currency values are used in calculations. Instead, use the decimal data type with at least four decimal places.

If you’re then going to use a numeric column to hold monetary values, then you may as well use an integer-type column as mentioned by others. It’s far more accurate to perform calculations on whole numbers than floating point numbers (which computers are notorious for having trouble with).

1 like
JussiMannisto's avatar

The decimal data type is a fixed-point type and exists for cases like this. Floating-point types are not suitable for money.

The only-ever-integer opinion is FUD and it doesn't make the problem fully go away. You still have to round things off when dealing with taxes or discounts.

We had a decimal column with two decimal places as our unit price column. Then we signed a contract with a high-volume client that required three decimal places for the unit price. It took one migration and a few validation rule tweaks to support more precise prices. If we had used cents everywhere, it would've been a nightmare.

You can use integers if you think they're adequate for your use case. If not, use the decimal type. I prefer decimal.

martinbean's avatar

You still have to round things off when dealing with taxes or discounts.

@JussiMannisto Yes, but tax authorities usually publish rules on what to do (i.e. round up or down, and when) for situations like that. HMRC here in the UK does.

Please or to participate in this conversation.