N
N
ninja op op2020-05-10 15:36:24
Laravel
ninja op op, 2020-05-10 15:36:24

How to round a number in LARAVEL Eloquent Model?

I write code:
let 's

$size = 1.8654949999;
$this->model->count += $size;
$this->model->save();


What method can be used to round up so that it is 1.86?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-05-10
@zkelo

You can write a mutator in the model that will round the number

Q
qvisn00, 2020-05-18
@qvisn00

Add a mutator to the model, which, when a value is assigned to a model attribute, will mutate (change) this value.

public function setCountAttribute($value)
    {
        $this->attributes['count'] = round($value, 2);
    }

https://laravel.com/docs/7.x/eloquent-mutators#def...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question