- 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)`.