Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Errors Laravel Package

laravelcollective/errors

LaravelCollective Errors offers flexible error and exception handling for Laravel applications. It supports customizable error views, logging hooks, and Whoops-style debugging, helping developers deliver clearer development feedback and friendlier production error pages with minimal setup.

View on GitHub
Deep Wiki
Context7

Getting Started

What this package actually does. laravelcollective/errors ships a MessageBag wrapper (extending Illuminate\Support\MessageBag) and a service provider that shares a $errors instance with views. If you are on Laravel 5.8+ or 6+, the framework already shares $errors from the session via ShareErrorsFromSession middleware, so this package is only load-bearing if you are (a) back-porting a legacy app still on 5.5–5.7, or (b) specifically want the older expressive formatting helpers this wrapper restores. Confirm your Laravel version before adopting.

Install and wire up.

composer require laravelcollective/errors

If auto-discovery is not available on your version, add the provider to config/app.php:

'providers' => [
    // ...
    Collective\Errors\ErrorsServiceProvider::class,
],

Publish the config if the package ships one:

php artisan vendor:publish --provider="Collective\Errors\ErrorsServiceProvider"

Where to look first.

  1. vendor/laravelcollective/errors/src/MessageBag.php — the wrapper. Confirm which methods it actually overrides (format(), first(), get()) versus which it inherits from Illuminate\Support\MessageBag.
  2. ErrorsServiceProvider::boot() — this is where $errors is shared via View::share('errors', ...) or a view composer. Understanding how it shares determines whether you get it on every view or only on ones with a session key.
  3. The config file — message formatting is the only meaningful knob.

First use case. A validator call in a controller:

$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
    return back()->withErrors($validator)->withInput();
}

Then in Blade, the wrapper gives you consistent $errors->first('field') behavior with whatever formatting style you configured — that is the point. If you can already do $errors->first('field') on your Laravel version without the package, you do not need this package.

Implementation Patterns

Blade rendering. The canonical use is field-level error output:

@if ($errors->has('email'))
    <span class="error">{{ $errors->first('email') }}</span>
@endif

Because the wrapper preserves the MessageBag interface, this works identically to stock Laravel — the package is a swap-in, not a new API. Do not rewrite existing Blade templates; adopt the package behind the existing $errors contract.

Custom output formatting. The wrapper's format() override is where the value lives. If you want errors rendered as field: message or with HTML escaping differences, configure the format string rather than post-processing in each view:

// config/errors.php-style pattern
'format' => ':field has :message',

Then everywhere you call $errors->first(...), the format applies. This is the main reason to reach for the package over vanilla — centralised message styling without touching every template.

API response integration. For JSON APIs, do not route through the wrapper — use $validator->errors()->toArray() directly. The package adds value for HTML views, not for machine responses. Keep the two paths distinct:

return response()->json([
    'errors' => $validator->errors()->getMessages(),
], 422);

Blade components / partials. Because $errors is shared globally, partials can use it without passing it explicitly. If you are migrating legacy partials that relied on the pre-5.5 global, this package restores exactly that expectation — which is convenient for a legacy upgrade but is a subtle source of "it works in this view but not that one" bugs if the share is conditional.

Validator coexistence. No wiring needed. The package does not replace Illuminate\Validation\Validator; it only wraps the resulting MessageBag. This means you can drop it in next to existing validation and it will pick up the errors automatically.

Testing. In feature tests, $response->assertSessionHasErrors([...]) still works because the underlying session shape is unchanged. Assert on rendered HTML with assertSee to catch formatting regressions when you tweak the config.

Gotchas and Tips

The package is largely redundant on modern Laravel. Laravel 5.8+ shares $errors automatically. Adopting this on 8/9/10/11 means you are adding a dependency that duplicates framework behavior and can silently conflict with the framework's own sharing. Verify whether you actually need it — often the fix is middleware config, not a package.

Version pinning is critical. Last release was March 2020. It was built against a specific Laravel/illuminate/support range. Check composer.json constraints before composer require; if your illuminate/support is newer than its ceiling, Composer will either resolve to an old Laravel or refuse. Do a composer require --dry-run first.

Order-of-share bugs. If both the package's provider and Laravel's ShareErrorsFromSession middleware share $errors, the last writer wins. Symptoms: errors appear in some views and vanish in others, or appear only on non-session requests. Debug by dd(app('view')->shared('errors')) inside a Blade partial. If you adopt the package, consider removing the framework's middleware-based sharing to avoid the double-bind — but be aware that affects every view in the app.

Config caching. If you publish a config file, remember php artisan config:cache in production will freeze your format string. Stale format strings after a config edit are a classic "why is the output wrong" issue; run config:clear locally, and re-run config:cache in deploy scripts.

Format string escaping. If your format inserts message text into HTML, the package does not necessarily escape it. Do not interpolate user-influenced field names or messages into {!! !!} blocks. Prefer {{ }} and let the format function produce plain text.

No upgrade path / no changelog. The 2020 release cadence means you should assume no security patches and no Laravel-major support. Treat it as frozen. If a framework upgrade breaks it, you own the fix — the wrapper is small enough to fork into app/Support/ in an afternoon, and that is likely the safer long-term move than depending on an unmaintained 130-star package.

Extension points. The MessageBag wrapper is the seam. If you need custom formatting not exposed by config, subclass the wrapper and bind it in the container or in your own view composer instead of editing the vendor class. The service provider is a single class and easy to swap for an app-level provider — the cleanest removal path is to lift its View::share call into AppServiceProvider::boot() and drop the package.

Debugging tips.

  • get_class($errors) in a Blade view confirms whether you have the package's wrapper or Laravel's stock MessageBag. If it is the stock class, the provider is not registered or is being overridden.
  • $errors->getBag('default')->messages() shows the raw array — useful when formatting obfuscates the shape.
  • If $errors->has('field') returns false but the validator clearly failed, the bag key and the field name differ (e.g., dot-notation for nested arrays: items.0.title). This is a MessageBag behavior, not the package, but it is the most common day-to-day confusion.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
laravel2580/idehelpers
delatbabel/viewpages
anavel/gettext
muhamadrezaar/highcharts
delatbabel/keylists
frankkessler/salesforce-laravel-oauth2-rest
anavel/foundation
wdmtech/enviricons
eii/laravel-captcha
muka/shape-reader
tdt/json
refinery29/php-cs-fixer-config
hotmeteor/eco-env
mvaliolahi/hydrate
wearede/tbcpay-php
parsnick/steak
thibaud-dauce/mattermost-php
multilinguals/apollo-client
antaresbr/collection
kr0lik/phpstan-rules