I've been playing around with Laravel using blade exclusively, but now I would like to venture my way into Inertia + Vuejs but I am having some trouble.
I was trying to display the value of the $totalAccounts variable to the user and it only works when I have the code inside the route like so:
Route::get('/dashboard', function () {
$user = Auth::user();
$totalAccounts = $user->accounts()->count();
return Inertia::render('Dashboard', [
'totalAccounts' => $totalAccounts,
]);
})->name('dashboard');
As soon as I extract the code into a controller such as DashboardController the whole page loads but its all blank for some reason.
I change the route to be something like:
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');
And this is the index() method of the DashboardController:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Account;
use Illuminate\Http\Request;
use Inertia\Inertia;
class DashboardController extends Controller
{
public function index()
{
// Get the currently authenticated user
$user = Auth::user();
// Count the total accounts for the authenticated user
$totalAccounts = $user->accounts()->count();
return Inertia::render('Dashboard', [
'totalAccounts' => $totalAccounts,
]);
}
}