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

Option Laravel Package

php-standard-library/option

Option type for PHP with explicit Some/None presence semantics, replacing nullable values so absence is handled intentionally instead of silently. Part of the PHP Standard Library.

View on GitHub
Deep Wiki
Context7

PHP Standard Library - Option provides a functional Option type with two variants, Some and None, replacing nullable values with explicit presence semantics. Instead of scattering null checks throughout your code, you wrap values in an Option and let the type track whether a value exists—making absence an intentional, first-class part of your API.

Ambiguous null returns disappear, accidental null errors are prevented, and signatures document their own contract.

  • Some/None variants for explicit presence or absence
  • Null-safe chaining via map, filter, flatMap
  • Pattern matching with fold/match helpers
  • Nullable interop for gradual adoption
  • Immutable, type-safe value objects
Frequently asked questions about Option
How do I install php-standard-library/option in a Laravel project?
Run composer require php-standard-library/option. It is a native PHP package with no Laravel dependencies, so no package service provider is required. Laravel's Composer autoloading makes the Option classes available.
What is the difference between Option and nullable types in Laravel?
Option makes absence explicit as None and presence as Some, while nullable types use null for absence. This removes nested isset/null checks and makes Some/None visible in method signatures. You can chain map, flatMap, and filter operations without repeated null guards.
Does php-standard-library/option integrate with Laravel Eloquent?
It can, but Eloquent does not return Option automatically. Wrap nullable query results with Option::fromNullable($model) and adapt legacy null-returning code at boundaries. Dynamic Eloquent properties may need wrappers rather than direct Option chaining.
Which PHP and Laravel versions does php-standard-library/option support?
The package requires PHP 8.0 or newer, which is needed for features such as match expressions. It has zero Laravel dependencies, so Laravel version compatibility is mainly determined by your app's PHP and Composer constraints. Check the package's composer.json for the exact supported ranges.
How do I wrap an Eloquent model in an Option?
Use Option::fromNullable($user) when the query result may be null; it yields Some for a model and None for null. Then chain map or filter for optional business logic instead of nested if checks. Keep raw Eloquent calls at the boundary and convert to Option before passing into domain code.
Can Option replace nullable request input handling in Laravel controllers?
You can wrap optional input with Option::from($request->input('optional_field')) to make absence explicit as None. This helps distinguish not provided from validation failures, which should remain 400-style responses. Keep Laravel validation for invalid data and use Option for presence semantics in your domain or API layer.
How should I test Option values in Laravel tests?
Assert presence explicitly, for example assertTrue($option->isSome()) or assertTrue($option->isNone()). Update tests that previously expected null to expect None and cover nested Option and missing-value edge cases. Add feature tests around request and Eloquent boundaries where you convert null to Option.
Is php-standard-library/option suitable for production Laravel applications?
It is a native PHP package with negligible runtime overhead compared with null checks; Some cases add slight object-wrapping memory that is usually insignificant at scale. It fits production code where explicit presence semantics reduce ambiguity, especially in domain and API layers. Benchmark critical paths and use it selectively rather than across all simple CRUD.
How do I configure PHPStan to work with Option types in Laravel?
Configure PHPStan to expect Option return types where nullable types were previously allowed. Add custom PHPStan or Nikita rules to flag null returns and enforce Option adoption, as native tooling may not infer all Option semantics. Expect more limited IDE autocompletion for Option methods than for native PHP types.
What are alternatives to php-standard-library/option and when should I avoid it?
Alternatives include plain nullable type hints with PHPStan ignores, or a hybrid approach using Option for domain models and null for simple APIs. Avoid Option for trivial CRUD forms where null is sufficient, and beware the cognitive load of monadic patterns like flatMap. Teams without functional-pattern experience should start with a targeted proof of concept.
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.
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings
coleus/apps
seampact/laravel
emmyconceptng/laravel-ng-postcode
jjsquady/round-robin