Weave 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

Result type for PHP that captures success or failure as a value, enabling controlled error handling without exceptions. Part of the PHP Standard Library.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Standardizing expected-failure handling across the product. The Result type turns "this operation can fail in a known way" into a value (Result::ok($user) / Result::err(ValidationError)) rather than a thrown exception. That is a product-level decision about API contracts and user-visible error behaviour, not just a code-style preference — it lets you promise clients structured failure payloads instead of opaque 500s.
  • Build vs. buy for an internal convention. The alternative is writing a house Result/Either class. This package is MIT, framework-agnostic, and already defines map, flatMap, match, mapErr, unwrap, unwrapErr, fromCallable. Buying is cheap; the real cost is the migration discipline, not the dependency.
  • Feature scoping: where the pattern pays off first. Highest-leverage Laravel seams are form/API validation, service and domain methods (processOrder(): Result), queue jobs (handle() branching to $this->fail()), and middleware short-circuits. This gives you a sequencing plan for rollout rather than a big-bang refactor.
  • Error taxonomy as a roadmap item. Deciding Result<User, ValidationError> vs. Result<User, ValidationError|PaymentFailed> is a schema decision that gates later observability work — structured error logging, per-error-type dashboards, retry policies on queues, and cleaner client SDKs.
  • Testability as a delivery-speed lever. Failures become assertable values (assertTrue($result->isFailure())), which reduces flaky edge-case testing on payment, auth, and onboarding flows. This supports a "reduce QA cycle time" objective.
  • Boundary hygiene between expected and unexpected failure. Exceptions stay for infrastructure faults (DB down, OOM); Result covers business outcomes. Codifying that rule prevents the pattern from becoming a second, parallel error system.

When to Consider This Package

Adopt if:

  • The product surface is API-, CLI-, or job-heavy, where clients and operators benefit from explicit, enumerable failure states.
  • Your codebase already suffers from deep try/catch nesting and inconsistent ad-hoc error arrays, and error shape varies per endpoint.
  • The team is PHP 8.1+ and comfortable with functional idioms (map, flatMap, immutability). Budget a short ramp-up; this is a genuine learning curve, not a drop-in.
  • You want framework-agnostic semantics that survive a move away from Laravel, or shared vocabulary with a non-Laravel service.
  • You are willing to wrap it behind your own thin adapter (ResultWrapper bound in the container) so the dependency is reversible.

Look elsewhere — or treat as an unproven spike — if:

  • Adoption signal matters to you. This split package shows 1 star, 0 dependents, and release notes that are one-line stubs pointing at the parent monorepo (php-standard-library/php-standard-library). That means independent adoption, issue traffic, and downstream breakage data are all effectively unmeasurable here. Score the parent project, not this package, when assessing maintenance risk.
  • You need rich error context — stack traces, causes, retry-after metadata, multi-error aggregation. Result carries plain values; you will need custom error classes, and the package's own guidance warns against unwrap() on Err.
  • The codebase is exception-coupled at its core (framework handlers, Eloquent events, third-party SDKs). Migrating those layers is expensive and low-value; a partial adoption will feel inconsistent.
  • You need first-party Laravel integration (validation exception mapping, HTTP response conversion). The package is deliberately framework-neutral, so glue code is yours to own and maintain.
  • Hot paths matter: object allocation in tight loops is a real, if modest, cost. Keep Result at boundaries, not inside inner loops.
  • You require an SLA or vendor accountability. There is none — plan for the option of replacing it.

Practical de-risking: pilot on one vertical slice (e.g., a single job + its controller), keep exceptions at the HTTP boundary by converting Result::err() to ValidationException/responses in one place, and track error-shape consistency before and after. If the pilot does not measurably reduce error-handling code or bug count, stop.

How to Pitch It (Stakeholders)

For executives: "Today, failures in our Laravel services are scattered — some throw, some return arrays, some silently log. That makes API error responses inconsistent, slows client integration, and makes production issues harder to triage. This package gives us one small, MIT-licensed, dependency-free convention for representing 'this can fail' as a value. It is a three-day pilot on one workflow, behind an internal wrapper so it is fully reversible. The payoff we are targeting is fewer integration questions, faster test cycles on edge cases, and a structured error format we can log and monitor. The cost is a dependency with low visible adoption — which is precisely why we pilot before committing and keep the wrapper."

For engineering: "Result makes expected failures explicit and composable: chain via flatMap, short-circuit naturally, recover with mapErr, and handle once with match. Keep exceptions for genuinely exceptional conditions — DB outages, misconfiguration — and use Result for validation, business rules, and external-call outcomes. Adopt it at the boundaries first: controllers, jobs, middleware, domain services. Do not call unwrap() without a success check. Type your failures (Result<User, ValidationError>), not Result<User, string>. Bind a thin ResultWrapper in the service container so we can swap the implementation without touching call sites. Expect a learning curve of roughly a sprint for anyone unfamiliar with map/flatMap; pair on the first few conversions."

What we will measure in the pilot: lines of error-handling code removed, number of distinct error shapes in API responses before vs. after, time to add a new validated endpoint, and any friction reports from the team. Decision gate at the end of the pilot: standardize, extend to one more workflow, or revert — all three are acceptable outcomes at this cost.

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.
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc