Use Eloquent for Space model's monthlyBalance method
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -20,7 +20,7 @@ class DashboardController extends Controller {
$balance = session('space')->monthlyBalance($currentYear, $currentMonth);
$recurrings = session('space')->monthlyRecurrings($currentYear, $currentMonth);
- $leftToSpend = $balance - $recurrings;
+ $leftToSpend = max($balance - $recurrings, 0);
$totalSpent = session('space')->spendings()->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [$currentYear, $currentMonth])->sum('amount');

MODIFY
app/Space.php
app/Space.php
@@ -46,19 +46,21 @@ class Space extends Model {
//
public function monthlyBalance($year, $month) {
- $query = DB::selectOne('
- SELECT (
- SELECT SUM(amount)
- FROM earnings
- WHERE space_id = :e_space_id AND YEAR(happened_on) = :e_year AND MONTH(happened_on) = :e_month
- ) - (
- SELECT SUM(amount)
- FROM spendings
- WHERE space_id = :s_space_id AND YEAR(happened_on) = :s_year AND MONTH(happened_on) = :s_month
- ) AS balance;
- ', ['e_space_id' => $this->id, 'e_year' => $year, 'e_month' => $month, 's_space_id' => $this->id, 's_year' => $year, 's_month' => $month]);
-
- return $query->balance;
+ $earningsAmount = Earning::where('space_id', $this->id)
+ ->whereYear('happened_on', $year)
+ ->whereMonth('happened_on', $month)
+ ->sum('amount');
+
+ $spendingsAmount = Spending::where('space_id', $this->id)
+ ->whereYear('happened_on', $year)
+ ->whereMonth('happened_on', $month)
+ ->sum('amount');
+
+ if (!$spendingsAmount) {
+ return $earningsAmount;
+ } else {
+ return $earningsAmount - $spendingsAmount;
+ }
}
public function monthlyRecurrings($year, $month) {

Use Eloquent for Space model's monthlyBalance method