spatie/or-else
Adds an OrElse trait that auto-creates “*OrElse” method variants for your class methods. These variants accept a fallback value that’s returned whenever the original method returns null or false, helping you avoid repetitive default-handling code.
orElse pattern, reducing ad-hoc implementations (e.g., ??, null checks, or ternary operators).null/empty responses with orElse (e.g., User::find($id)->orElse(new User())).Post::with('author')->find($id)->author->orElse(new Author())).users->filter()->orElse(collect([]))).??, empty(), or is_null() checks).laravel-optional.nullsafe operators (?->) over traits.tap(), when()) over method chaining → Explore spatie/array-to-object or laravel-collective/macroable.For Executives:
"This package lets our PHP/Laravel teams replace repetitive ‘null check’ code with a single, reusable orElse method—like Ruby’s ||= but for objects. It’s a 10-minute setup that saves hours of debugging inconsistent fallbacks in APIs, queries, or collections. Think of it as a ‘default value’ superpower for our codebase, with zero risk (MIT license, no dependencies). Ideal for projects where data reliability matters, like user profiles or payment processing."
For Engineers:
*"Spatie’s or-else trait adds orElse() to any class, turning this:
$user = User::find($id) ?? new User();
into this:
$user = User::find($id)->orElse(new User());
Pros:
?? or empty() checks, or need to standardize fallback logic in a Laravel monolith."*For Architects: *"This is a low-risk, high-reward addition for teams prioritizing:
?? sprawl).?-> suffices.
Recommendation: Pilot in a non-critical module (e.g., API responses) to measure developer satisfaction before wider adoption."*How can I help you explore Laravel packages today?