Calculate a user's most expensive tag and display it on dashboard
MODIFY
app/Http/Controllers/DashboardController.php
app/Http/Controllers/DashboardController.php
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
+use DB;
class DashboardController extends Controller {
public function index() {
@@ -20,6 +21,23 @@ class DashboardController extends Controller {
->whereRaw('MONTH(happened_on) = ?', [date('m')])
->sum('amount');
+ $mostExpensiveTag = DB::select('
+ SELECT
+ tags.name AS name,
+ SUM(spendings.amount) AS amount
+ FROM
+ tags
+ LEFT OUTER JOIN
+ spendings ON tags.id = spendings.tag_id
+ WHERE
+ tags.user_id = ?
+ GROUP BY
+ tags.id
+ ORDER BY
+ SUM(spendings.amount) DESC
+ LIMIT 1;
+ ', [$user->id])[0];
+
$recentEarnings = $user
->earnings()
->orderBy('happened_on', 'DESC')
@@ -37,6 +55,7 @@ class DashboardController extends Controller {
'spendingsToday' => $spendingsToday,
'spendingsMonth' => $spendingsMonth,
+ 'mostExpensiveTag' => $mostExpensiveTag,
'recentEarnings' => $recentEarnings,
'recentSpendings' => $recentSpendings

MODIFY
resources/views/dashboard.blade.php
resources/views/dashboard.blade.php
</div>
</div>
</div>
+ <div class="row__column">
+ <div class="box">
+ <div class="box__section">
+ <h2>{{ $mostExpensiveTag->amount ? $mostExpensiveTag->name : '-' }}</h2>
+ <div class="mt-1">Most expensive tag</div>
+ </div>
+ </div>
+ </div>
</div>
<div class="row gutter spacing-bottom-large">
<div class="column">

Calculate a user's most expensive tag and display it on dashboard