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

Option Laravel Package

php-standard-library/option

Option type for PHP with Some/None to replace nullable values with explicit presence semantics. Helps avoid null checks, clarifies intent, and models optional data safely. Part of PHP Standard Library; see docs and contributing links.

View on GitHub
Deep Wiki
Context7

Option type (Some/None) replacing nullable types with explicit presence semantics

Frequently asked questions about Option
How do I install the Option type in a Laravel project?
Run `composer require php-standard-library/option` in your project root. No Laravel-specific dependencies exist, so it integrates seamlessly with existing codebases. The package follows PSR-4 autoloading standards, so no additional configuration is needed for basic usage.
Can I use Option with Laravel Eloquent models?
Yes, wrap Eloquent results explicitly using `Option::fromNullable($model)` or `Option::from($model, fn() => null)`. For query builders, chain methods like `Model::where(...)->first()` and then wrap the result. Avoid mixing raw nulls and Options in the same domain to prevent ambiguity.
What Laravel versions does this package support?
The package is framework-agnostic and works with any PHP 8.1+ project, including Laravel 9.x and 10.x. No Laravel-specific dependencies mean it’s compatible with all modern Laravel releases. Test thoroughly in your target version, as some dynamic Laravel features (e.g., magic methods) may require adapters.
How do I handle Option types in Laravel’s request validation?
Replace nullable validation rules (e.g., `'field' => 'nullable|string'`) with explicit Option logic. Use `Option::from($request->input('optional_field'))` to wrap inputs, then chain methods like `map()` or `filter()` for conditional processing. This avoids nested `isset()` checks and makes intent clearer.
Will using Option improve performance in Laravel?
The runtime overhead is negligible—Option adds type safety without significant memory or CPU costs. However, wrapping every nullable value (e.g., `Option::from($user)`) introduces minor object instantiation. Benchmark critical paths to confirm the tradeoff is worth the bug reduction, especially in high-traffic APIs.
How do I test code that returns Option types in Laravel?
Use assertions like `assertTrue($option->isSome())` or `assertEquals($value, $option->unwrap())` in PHPUnit. For edge cases, test `None` handling (e.g., `assertTrue($option->isNone())`) and chained operations (e.g., `map()` on `None`). Mock dependencies returning `Option` to simulate optional data flows.
What’s the best way to migrate from nullable types to Option in Laravel?
Start with a single domain (e.g., `Order` entity) and refactor return types incrementally. Use adapters (e.g., `OptionAdapter::fromNullable($legacyValue)`) to coexist with old code. Update method signatures (e.g., `?User → Option<User>`) and leverage IDE refactoring tools to automate changes.
Are there alternatives to Option for handling optional data in Laravel?
For simple cases, nullable types (`?User`) or `@phpstan-ignore` annotations may suffice. Libraries like `league/value-object` or `spatie/array-to-object` offer similar patterns but lack the functional chaining (e.g., `map`, `filter`) that Option provides. Weigh the tradeoff between explicit semantics and complexity.
How do I integrate Option with Laravel’s service container?
Bind Option-wrapped services in your `AppServiceProvider` (e.g., `app->bind(Option::class, fn() => new Option(...))`). For repositories, return `Option<User>` instead of `?User` and resolve dependencies via constructor injection. Laravel’s DI will handle the rest without additional configuration.
What tools or IDE support are available for Option in Laravel?
IDE autocompletion for Option methods is limited compared to native PHP. Use PHPStan’s `Option` rules to enforce type safety (e.g., `phpstan --level=max`). For debugging, log `isSome()`/`isNone()` states or use custom error handlers to trace Option chains. Static analysis will catch most integration issues early.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata