Get rid of mutator for amount field in Earning/Spending models and move logic for converting to controllers
MODIFY
app/Earning.php
app/Earning.php
@@ -21,17 +21,6 @@ class Earning extends Model {
return ($secondsDifference / 60 / 60 / 24) . ' days ago';
}
- // Mutators
- public function setAmountAttribute($value) {
- if (strpos($value, '.')) {
- $mutated = str_replace('.', '', $value);
- } else {
- $mutated = $value * 100;
- }
-
- $this->attributes['amount'] = $mutated;
- }
-
// Relations
//
}

MODIFY
app/Http/Controllers/EarningsController.php
app/Http/Controllers/EarningsController.php
@@ -37,7 +37,7 @@ class EarningsController extends Controller {
$earning->user_id = Auth::user()->id;
$earning->happened_on = $request->input('date');
$earning->description = $request->input('description');
- $earning->amount = $request->input('amount');
+ $earning->amount = (int) ($request->input('amount') * 100);
$earning->save();

MODIFY
app/Http/Controllers/SpendingsController.php
app/Http/Controllers/SpendingsController.php
@@ -42,7 +42,7 @@ class SpendingsController extends Controller {
$spending->tag_id = $request->input('tag_id');
$spending->happened_on = $request->input('date');
$spending->description = $request->input('description');
- $spending->amount = $request->input('amount');
+ $spending->amount = (int) ($request->input('amount') * 100);
$spending->save();

MODIFY
app/Spending.php
app/Spending.php
@@ -21,17 +21,6 @@ class Spending extends Model {
return ($secondsDifference / 60 / 60 / 24) . ' days ago';
}
- // Mutators
- public function setAmountAttribute($value) {
- if (strpos($value, '.')) {
- $mutated = str_replace('.', '', $value);
- } else {
- $mutated = $value * 100;
- }
-
- $this->attributes['amount'] = $mutated;
- }
-
// Relations
public function tag() {
return $this->belongsTo('App\Tag');

Get rid of mutator for amount field in Earning/Spending models and move logic for converting to controllers