Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aone24's avatar

undifined variable $stats

hello i'm getting Undifined variable $stats, (this is just test code I wrote) I don't think my logic is correct, it must not be.

this is my InventoryController.php $stats = [ 'total_items' => InventoryItem::count(), 'pending_items' => InventoryItem::where('Status', 'Pending')->count(), 'accepted_items' => InventoryItem::where('Status', 'Accepted')->count(), 'rejected_items' => InventoryItem::where('Status', 'Rejected')->count(), ];

and route Route::prefix('inventory')->name('inventory.')->group(function () { Route::get('/dashboard', [InventoryController::class, 'dashboard'])->name('inventory.dashboard'); Route::get('/create', [InventoryController::class, 'create'])->name('inventory.create'); Route::post('/store', [InventoryController::class, 'store'])->name('inventory.store'); Route::get('item/{item}', [InventoryController::class, 'show'])->name('inventory.show'); Route::post('item/{item}/accept', [InventoryController::class, 'accept'])->name('inventory.accept'); Route::post('item/{item}/reject', [InventoryController::class, 'reject'])->name('inventory.reject'); });

i'm use @extends('layouts.app') in dashboard.blade.php (resources/views/inventory/dashboard.blade.php) Total Items {{ $stats['total'] }}

my app.blade.php (resources/views/layouts/app.blade.php)

<main class="max-w-7xl mx-auto py-6 px-4">
    @if(session('success'))
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
            {{ session('error') }}
        </div>
    @endif

    @yield('content')
</main>
0 likes
3 replies
Shivamyadav's avatar

First tell me from where you are returning the dashboard.blade.php

  1. Are you passing your data $stats to that dashboard view ..

And make sure to place your code in between three start and three end backticks ``` like this .

jayandholariya's avatar

@aone24 Just to clarify — are you passing the $stats variable to your view like this?

return view('inventory.dashboard', compact('stats'));

Without this line in your dashboard() method of InventoryController, the view won’t receive the $stats data, which will cause the "Undefined variable" error you're seeing in your Blade file.

Please or to participate in this conversation.