Weave Code
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

Laravel Or Abort Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require spatie/laravel-or-abort
    

    (Note: Despite being archived, this remains a lightweight, functional package.)

  2. Usage in Eloquent Models: Add the trait to your model:

    use Spatie\OrAbort\OrAbort;
    
    class User extends Model
    {
        use OrAbort;
    }
    
  3. First Use Case: Replace findOrFail() with findOrAbort() for custom HTTP status codes:

    $user = User::findOrAbort($id, 404); // Returns 404 if not found
    $user = User::findOrAbort($id, 422); // Returns 422 (e.g., for validation-like failures)
    

Implementation Patterns

Core Workflows

  1. Dynamic Abort Codes: Useful for domain-specific failures (e.g., 403 for unauthorized access):

    $resource = Resource::where('owner_id', auth()->id())->firstOrAbort(403);
    
  2. Service Layer Integration: Encapsulate logic in services to centralize abort handling:

    class UserService {
        public function getUserOrFail($id) {
            return User::findOrAbort($id, 404);
        }
    }
    
  3. Custom Logic with OrAbort: Extend beyond Eloquent (e.g., API responses):

    $data = Cache::get('key') ?: abort(404);
    // Or with the trait:
    $this->assertCacheExistsOrAbort('key', 404);
    

Integration Tips

  • Laravel 5.5+: Use use OrAbort; directly in models/controllers.
  • Legacy Projects: For older Laravel versions, manually include the trait file:
    use Spatie\OrAbort\OrAbort as OrAbortTrait;
    

Gotchas and Tips

Pitfalls

  1. Archived Package:

    • No new features/bugfixes. Use at your own risk (e.g., Laravel 10 compatibility may break).
    • Consider forking or replacing with native abort() + optional():
      $model = Model::find($id) ?: abort(404);
      
  2. Method Overrides:

    • The trait overrides findOrFail(), firstOrFail(), etc. Ensure no naming conflicts in your codebase.
  3. HTTP Status Codes:

    • Invalid codes (e.g., 999) will throw exceptions. Validate inputs:
      $status = is_int($status) && $status >= 400 && $status <= 599 ? $status : 500;
      

Debugging

  • Silent Failures: If OrAbort isn’t triggering, check:
    • Trait is properly used (use OrAbort;).
    • Method calls match exactly (e.g., findOrAbort, not findOrAbortIf).

Extension Points

  1. Custom Abort Logic: Override the trait’s abort() method for logging or analytics:

    protected function abort($code) {
        \Log::warning("Aborted with code: $code");
        http_response_code($code);
        exit;
    }
    
  2. Non-Eloquent Models: Add static methods to other classes:

    class ApiResponse {
        use OrAbort;
    
        public static function getDataOrAbort($key, $status = 404) {
            return self::getData($key) ?: self::abort($status);
        }
    }
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport