13en's avatar
Level 2

Adding 0.000005 to 0.000005?

I've tried mostly everything basically i just want to add the values 0.000005 and 0.000005 together increasing every time a form is submitted. So for example 0.000005 / 0.000010 / 0.000015 and so on but i don't absolutely no idea how to get this to work.

Table 1 cost contains the value to start the sum from

    Schema::create('monsters', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->default('orphan');
        $table->longText('description');
        $table->decimal('cost')->default('0.000005');
        $table->string('backgroundcolor')->default('#fae5d6');
        $table->timestamps();
    });

and heres the code for the increase of the value

Monster::where(['id'=>$monster_id])->increment('cost', +0.000005);

If anyone could point me in the right direction it would be much appreciated, i've tried nearly every table type known to man, downloaded packages, and still can't solve this.

0 likes
7 replies
robrogers3's avatar

you would think it does,

but here's the signature, looks otherwise. weird.

@ 13EN what happens your way?

but yeah, you can probably just do an update $monster = Monster::find($id); $monster->update('cost', $monster->cost + 0.000005); etc.

jimmck's avatar

@13en Are you trying to update all of the rows in your DB where the cost is > .0000005 + .000005 ? Using a formula as a part of a where clause? My initial thought was/is can't work. But apparently in MySQL there is way. Which looking at the solution with an alias and HAVING seems interesting. Note I have not tried this yet, going to to so as its interesting. Remember Eloquent is an ORM not an extension to SQL. All Eloquent code turns into SQL and may further process the returned result sets. Of course any searches apply to result sets. But interesting question! I will be check this further. Good Luck.

https://stackoverflow.com/questions/21353959/sql-query-by-adding-two-columns-in-where-clause

Here is search URL

https://www.google.com/search?q=mysql+where+clause+with+formula&oq=mysql+where+clause+with+formula&aqs=chrome..69i57.20663j0j7&sourceid=chrome&ie=UTF-8

Snapey's avatar
Snapey
Best Answer
Level 122

to be honest, i would go for the precision of integers and then divide the result in the view.

making the column an integer or big int is more predictable and you should have no problems incrementing by 5 instead of 0.000005

Then in the model create an accessor that divides the value by 100,000 when it is needed for a view.

basically the same way I would deal with currencies

1 like
burlresearch's avatar

You may want to reconsider the database storage type of the field. Different databases and even versions may have different behaviors when it comes to decimal field types, but when you're dealing in increments of 5 millionths - you may experience issues due to the type.

Did you try declaring your field as double?

        $table->double('cost')->default('0.000005');

That could be worth a shot. Otherwise, I tend to agree with @Snapey and store integer increments and then transform the rendered value at display time, in the view.

13en's avatar
Level 2

Thanks Shapey, that worked like magic

Please or to participate in this conversation.