spatie/laravel-or-abort
Adds an OrAbort trait to Laravel models to abort with a chosen HTTP status when common calls fail (e.g., findOrAbort($id, 500) instead of returning null/false). Note: this package is no longer maintained.
findOrFail() or firstOrFail() methods aren’t available or require customization (e.g., non-standard HTTP codes).OrAbort was previously used as a convention (e.g., 404 for missing resources, 500 for validation failures).findOrAbort implementations in the codebase with a single, maintained trait (even if archived, it’s a drop-in replacement).findOrAbort(422) for validation errors) when the package already exists.OrFail pattern in legacy contexts.Avoid if:
findOrFail(), firstOrFail(), or existsOrFail() methods are built-in and preferred.spatie/laravel-query-builder or Laravel’s own Illuminate\Database\Eloquent\Builder for advanced query logic.findOrAbortIf($condition, $code)), which this trait doesn’t support.Consider if:
422 for validation) in legacy APIs.findOrFail overrides.For Executives:
"This is a low-risk, high-impact fix for legacy systems where custom findOrAbort logic is duplicated across the codebase. By adopting this archived but battle-tested package, we standardize error handling (e.g., consistent 404/500 responses) without rewriting logic. It’s a one-time cleanup for systems we’re not actively modernizing, saving dev time while reducing technical debt. The MIT license and zero dependents mean no vendor lock-in."
For Engineering:
*"This trait fills a gap in pre-5.3 Laravel where findOrFail didn’t exist or needed custom HTTP codes. It’s a drop-in replacement for ad-hoc try-catch blocks or manual abort() calls. Caveats:
findOrAbort($id, $code)—don’t use for complex logic.Proposed action: Add to composer.json as a dev dependency, update legacy controllers to use the trait, and document its use as a temporary solution in the architecture docs."*
How can I help you explore Laravel packages today?