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

Result Laravel Package

php-standard-library/result

A lightweight Result type for PHP that represents success or failure as a value, enabling controlled error handling without exceptions. Helps you return, compose, and inspect outcomes explicitly for safer, predictable application flow.

View on GitHub
Deep Wiki
Context7

Result type capturing success or failure as a value for controlled error handling

Frequently asked questions about Result
How does `php-standard-library/result` improve error handling in Laravel APIs compared to exceptions?
It replaces try-catch blocks with explicit `Result::match()` calls, letting you return HTTP responses directly (e.g., `Result::match(fn($data) => response($data), fn($err) => response($err, 400))`). This makes error flows predictable and testable without nested conditionals.
Can I use this package with Laravel’s built-in validation (e.g., `Validator::validate()`)?
No, but you can wrap validation calls in `Result::fromCallable()` to convert boolean outcomes into `Result::Ok()` or `Result::Err()`. For example: `Result::fromCallable(fn() => Validator::validate($data))`.
What Laravel versions and PHP versions does this package support?
The package requires **PHP 8.1+** (for union types and named arguments) and works with **Laravel 9+**. It’s fully compatible with Laravel 10’s functional programming features like `collect()` and `tap()`.
How do I migrate from exceptions to `Result` in existing Laravel code?
Use `Result::fromThrowable()` to convert caught exceptions into `Result::Err()`, or refactor critical paths (e.g., API controllers) to return `Result` types directly. Start with new features to minimize disruption.
Does this package work with Laravel queues/jobs for async error handling?
Yes. Dispatch jobs with `Result` types (e.g., `Bus::dispatch(new ProcessOrder($order))->then(fn($result) => $result->match(...))`), then handle failures in the `then()` callback or job failure callbacks.
Can I enforce `Result` usage in specific Laravel layers (e.g., API controllers) with static analysis?
Not natively, but you can use **PHPStan** or **Psalm** to annotate return types (e.g., `@return Result<User, ValidationError>`) and create custom rules to flag violations. IDE plugins like PHPStorm can also highlight `Result`-returning methods.
What’s the performance impact of using `Result` vs. exceptions in Laravel?
Minimal overhead—`Result` is immutable and lightweight. For hot paths (e.g., high-frequency queries), cache `Result` instances or use `Result::unwrap()` for performance-critical sections, but prioritize readability.
How do I log `Result::Err()` failures in Laravel?
Use middleware or a service provider to intercept `Result` types and log errors (e.g., `Result::match(..., fn($err) => Log::error($err))`). For HTTP requests, combine with Laravel’s `app['log']` container binding.
Are there alternatives to `php-standard-library/result` for Laravel?
Yes: **`league/value-object`** (for custom error types), **`spatie/laravel-result`** (Laravel-specific wrapper), or **`phpoption/phpoption`** (similar but less composable). This package stands out for its **zero dependencies** and **PHP 8.1+ type safety**.
How do I test code that returns `Result` in Laravel’s PHPUnit?
Replace `expectException()` with assertions like `expect($result->isErr())` or `expect($result->unwrap())->toEqual($expected)`. For happy paths, use `expect($result->unwrap())->toBeInstanceOf(User::class)`.
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope