Create relationship to spendings from budget model
MODIFY
app/Budget.php
app/Budget.php
@@ -4,8 +4,27 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
+use App\Spending;
+
+use Auth;
+
class Budget extends Model {
public function tag() {
return $this->belongsTo('App\Tag');
}
+
+ public function spendings() {
+ $user = Auth::user();
+
+ $date = $this->attributes['date'];
+
+ $month = date('m', strtotime($date));
+ $year = date('Y', strtotime($date));
+
+ return Spending::where('user_id', $user->id)
+ ->whereMonth('date', $month)
+ ->whereYear('date', $year)
+ ->where('tag_id', $this->attributes['tag_id'])
+ ->get();
+ }
}

MODIFY
resources/views/reports/show.blade.php
resources/views/reports/show.blade.php
@foreach (App\Budget::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->get() as $budget)
<tr>
<td>{{ $budget->tag->name }}</td>
- <td>@include('partials.currency') {{ App\Spending::where('user_id', Auth::user()->id)->whereMonth('date', $month)->whereYear('date', $year)->where('tag_id', $budget->tag->id)->sum('amount') }} of @include('partials.currency') {{ $budget->amount }}</td>
+ <td>@include('partials.currency') {{ $budget->spendings()->sum('amount') }} of @include('partials.currency') {{ $budget->amount }}</td>
</tr>
@endforeach
</tbody>

Create relationship to spendings from budget model