Created
June 1, 2017 19:07
-
-
Save calebporzio/0a8353962fc943a5bf0cf40feeb27eea to your computer and use it in GitHub Desktop.
A handy accessor and mutator for storing amounts in cents and accessing and setting them in dollars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class [Insert Name Here] extends Model | |
{ | |
protected $guarded = []; | |
function getAmountInDollarsAttribute($dollars) | |
{ | |
return sprintf('$%01.2f', $this->amount_in_cents / 100); | |
} | |
function setAmountInDollarsAttribute($dollars) | |
{ | |
$dollars = str_replace('$', '', $dollars); | |
$this->amount_in_cents = (int) (((float) $dollars) * 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment