Undefined Variable in Laravel Blade? Check These 5 Spots Before You Touch Anything Else
Every Laravel developer meets this error sooner or later: ErrorException: Undefined variable $products The page was fine yesterday, or it never worked at all, and the message gives you almost nothing to go on. Here is
Every Laravel developer meets this error sooner or later:
ErrorException: Undefined variable $products
The page was fine yesterday, or it never worked at all, and the message gives you almost nothing to go on. Here is the thing most beginners miss: the error is almost never a Blade problem. It is a data-passing problem. A Blade template can only use variables that were explicitly handed to it. Your controller, your model, and your other views do not share scope with it.
Before you start changing code, check these five spots in order. In my experience the bug is in one of them almost every time.
The controller never passed it. The controller fetches the data and then renders the view without it. Open the method behind the route and look at the view() call. If the variable is not in the second argument, that is your bug. Pass it with an array, compact(), or with() — all three work, just pick one style per project.
The name does not match. The controller passes product and the view loops over $products. Or compact('product') is used while the variable is called $products. Compare the names character by character: singular against plural, camelCase against snake_case. This typo hides in plain sight and takes thirty seconds to confirm.
The variable lives in the layout. Your controller passed the data to the child view, but the error points at layouts/app.blade.php — a sidebar or nav that lists categories on every page. A page that never passes $categories crashes the layout. Fix it once with View::share() or a view composer in AppServiceProvider, not by copying the same compact() call into every controller.
An include did not get it. @include inherits the parent's variables, but only the ones the parent actually has. If a partial needs something extra, pass it explicitly: @include('partials.product-card', ['product' => $product]). Components work the same way — they only see the attributes in the tag.
The route renders a different view. Sometimes the controller is fine and the Blade file is fine, but they never meet. A method returns view('shop.list') while you are editing shop/index.blade.php. Run php artisan route:list and check which view the route actually renders.
Two more habits save you next time. First, initialise variables that are only set inside an if branch — $results = collect(); before the condition — so the template always has something to loop over. Second, guard genuinely optional data with @isset or ??, but do not use guards to hide a missing pass: a page that must have $products should fail loudly, not render empty.
I wrote the full walkthrough with working code for each fix here: https://rajadevsolutions.online/errors/how-to-fix-undefined-variable-error-in-laravel-blade
It covers the five causes above in detail, plus the difference between 'Undefined variable' and 'Attempt to read property on null', which usually shows up right after you fix the first one.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.