$errors is undefined in a Laravel Blade view
Undefined variable $errorsShort 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.
Fix it fast
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=register2Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
What does the error look like?
Undefined variable $errorsErrorException: 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.php2Fix
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=registerConfirmed by laravel.com
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/*.php2Fix
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=registerConfirmed by laravel.com
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 says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack 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 fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack 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 fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix