Friday, March 11, 2022

[FIXED] Laravel - Number with 4 decimals

Issue

I want to store 1 number with 4 decimals, in my database.

If i use float i can add only 2 decimals

$table->float('sell');

enter image description here


If I try with decimal i get an error

$table->decimal('sell', 1, 4);

The first number must be greater than or equal to the second number.

SQLSTATE[42000]: Syntax error or access violation: 1427 For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '  

sell'). (SQL: create table customers (id int unsigned not null auto_increment primary key, sell decimal(1, 4) not null,
created_at timestamp null, updated_at timestamp null) default character set utf8 collate utf8_unicode_ci)

Any help?

Thanks


Solution

Use the following;

$table->decimal('foo', 5, 4);

The first parameter is the total number of numbers, the second parameter is the "decimal precision".



Answered By - Darren Taylor

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.