🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page

Was this helpful?

  1. Laravel

Using a mutator to save currency

When user enters Pounds & Pence or Dollars & Cents

PreviousShow custom page when email verification link expiredNextUsing Spatie Valuestore to hold frequently accessed settings

Last updated 4 years ago

Was this helpful?

When working with currencies, it's good practice to store the value in the lowest denomination (eg cents) and then revert to normal currency format when displaying the value. This is to avoid the use of floating point numbers and rounding issues when calculating VAT percentages.

If you capture the value from the user, then they will present you will something like 12.75 when you need to save 1275 in the database.

Eloquent accessors and mutators can be used for this conversion in each direction. The example below uses Pounds, but the principle is the same for Dollars or Euro.

In the eloquent model

    public function setPoundsAttribute($value)
    {
        $this->each = strval($value*100);
    }

    public function getPoundsAttribute()
    {
        return number_format($this->each/100,2);
    }

each is the column storing the pence value.

Now we can save the value to the model in pounds format eg $item->pounds = '12.75'

Be sure to add the mutated field (pounds) to your $fillable attribute if you are using mass assignment protection.

This question when posed on Laracasts produced quite a few answers and alternative ways, the main issue being to avoid floating point conversion issues with certain values, eg 19.99

https://laracasts.com/discuss/channels/general-discussion/int-conversion-issue