Laravel Nova: Modify value (muliply) before saving to database

Viewed 2026

I am using Laravel Nova to administrate my project. However, I save certain values in my database like percentages or prices as x*100. Why I am doing this? Because when saving numbers *100 I can store them as int instead of double or float.

However, this leads to the problem that I am seeing the number which is saved and multiplied by 100 in the database on my Laravel Nova Admin dashboard. I mean I can bypass that by using resolveUsing or displayUsing (read here). Then e.g. 1530 will be presented as 15,30 when diving by 100 which is correct.

The only problem with this is when typing 100 in the percentage field e.g. it also gets saved as 100 and not as 10000 (100*100). Does anybody knows how I can implement this?

I also tried to use eloquent observers. But this doesn't work because I end up in a infinity loop.

Kind regards and thank you!

1 Answers

Use model accessor and mutator. https://laravel.com/docs/master/eloquent-mutators#defining-a-mutator

If you will use mutator and accesor, you should remove resolveUsing and displayUsing in your resource fields declaration.

public function setAmountAttribute($value)
{
    $this->attributes['amount'] = $value * 100;
}

public function getAmountAttribute($value)
{
    return $value / 100;
}
Related