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!
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!
Share Improve this question asked Mar 14 at 4:56 Filbert UmbawaFilbert Umbawa 114 bronze badges 11- 1 Money has been deprecated since probably the fall of soviet union, why would you want to use it in new code? – siggemannen Commented Mar 14 at 7:21
- What do you mean? I asking about migration with money datatype in Laravel for SQL Server. – Filbert Umbawa Commented Mar 14 at 7:29
- i mean, dont't use money datatype. Use numeric – siggemannen Commented Mar 14 at 7:51
- 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? – Filbert Umbawa Commented Mar 14 at 8:53
- 2 blog.greglow/2018/01/15/… – jarlh Commented Mar 14 at 9:14
1 Answer
Reset to default -1Laravel does not provide a direct money column type.
I think using decimal is the better option, but if u really need the money type then use raw SQL