Problems saving decimal value in Database (Laravel + Postgres)

Asked

Viewed 199 times

1

I’m using Laravel + Postgres and I’m hoping to receive a decimal value, and in Migration I did so: $table->decimal('valor', 10, 2);

When I get this amount, I get something like, $1,500.99 Before sending to the bank, I made a mutator that takes anything that is NOT a number, but when it arrives at the postgres it arrives like this: 150099.00 . That is, Added two zeros at the end.

Does anyone know how I can solve this problem?

  • Maybe your mutator is removing characters that are not numbers leaving it as a complete integer

  • You can use https://www.php.net/manual/en/function.number-format.php

  • put the mutator in the question please to check what you are doing wrong

  • I am calling this Function in the mutator: Function parse_money($number) { $number = preg_replace('/[ 0-9]+/', '', $number); Return number_format($number, 2, ',', '.'); }

  • first I’m taking out the R$ that arrives, and then formatting but it hasn’t worked yet

1 answer

1


If you send from the screen: R$1.500,99 need to use the str_replace as follows:

function parse_money($number) 
{
    return str_replace(['R$','.',','],['','','.'], $number);
}

that is, you need to convert to decimal value for the database settings.

Online Example

Ref. str_replace

Browser other questions tagged

You are not signed in. Login or sign up in order to post.