Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

$errors is undefined in a Laravel Blade view

ErrorUndefined variable $errors

Short answer

Laravel shares $errors with views through the ShareErrorsFromSession middleware, which belongs to the web group. A view rendered from a route that does not use that group, or after the middleware was removed, has no $errors. Put the route in the web group or stop reading $errors there.

  • Applies to Laravel 11, Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the middleware the failing route really runs. If ShareErrorsFromSession is not there, that is the cause.

    php artisan route:list -vv --path=register
  2. 2Match what you see, then jump to its fix

    1. 1The route lives in routes/api.php, or in a custom Route::group that has no web middleware.The route is registered outside the web middleware groupFix
    2. 2The route is in the web group, but the middleware list in route:list does not show ShareErrorsFromSession.A middleware change removed ShareErrorsFromSession or the sessionFix

What does the error look like?

  • Undefined variable $errors
  • ErrorException: Undefined variable: errors

Cause 1 of 2

The route is registered outside the web middleware group

1Check

Only routes/web.php gets the web group by default. Routes in routes/api.php are stateless and use the api group, so no session errors are shared.

php artisan route:list -vv
grep -n "withRouting" -A 6 bootstrap/app.php

2Fix

Move the route into routes/web.php, or wrap it in the web middleware.

Route::middleware('web')->group(function () {
    Route::get('/register', [RegisterController::class, 'show']);
});

3Verify

Open the page again; the view renders and $errors->any() works.

php artisan route:list -vv --path=register

Confirmed by laravel.com

Did this fix it?

Cause 2 of 2

A middleware change removed ShareErrorsFromSession or the session

1Check

The web group is made of EncryptCookies, StartSession, ShareErrorsFromSession and others (-vv expands the group). A web(remove: [...]) call, or a withoutMiddleware on the route, takes them out.

grep -n -E "web\(|withoutMiddleware" bootstrap/app.php routes/*.php

2Fix

Delete the remove entry for ShareErrorsFromSession , or take the route out of that group on purpose and do not use $errors in its view.

// bootstrap/app.php: remove an entry like this one
$middleware->web(remove: [
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);

3Verify

route:list -vv shows ShareErrorsFromSession on the route.

php artisan route:list -vv --path=register

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Keep form pages in routes/web.php so they get sessions and validation errors.
  • Run php artisan route:list -v after changing middleware to see the real list.
  • Do not copy a form layout that reads $errors into a view that an API route renders.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.