Nullable wrapper to explicitly handle nullability, reducing runtime errors and improving code clarity.map, filter, getOrElse) to chain transformations safely, which fits modern Laravel apps using collections, API layers, or domain-driven design (DDD).Nullable::from($request->input('optional_field'))).Nullable::from($user->profile)->getOrElse(fn() => Profile::default())).isPresent(), isAbsent()).Nullable<T> (via PHPDoc or IDE plugins like PHPStan).DB::select()->first() → Nullable::from($result)).map only executes if the value is present). Benchmark critical paths (e.g., bulk operations) to ensure no regression.unwrap() sparingly).@var Nullable<Model>) improve IDE autocompletion but won’t catch all null issues. Pair with PHPStan or Psalm for stricter checks.Nullable objects may obscure stack traces. Use unwrap() or toString() judiciously in logs.if ($var === null) to if ($nullable->isAbsent()) in existing logic.Nullable<User>) vs. API contracts (e.g., JSON responses)?Nullable logic? Use property-based testing (e.g., PestPHP) to verify edge cases (e.g., nested Nullable objects).Nullable wrappers or raw null values?unwrap() failures? Consider a global error handler for NullableException.Nullable in high-throughput endpoints (e.g., webhooks, batch jobs).Nullable::from($var)->map(...) over if ($var) { ... }").protected ?string $optionalField; → protected Nullable $optionalField).Nullable objects to JSON (e.g., JsonResource::withoutWrapping() for absent values).Nullable::from($request->input('age'))->filter(fn($age) => $age > 0)).Nullable::from($client->get('user')->profile)).Nullable::from($job->payload)).Phase 1: Pilot Project
if ($var === null) with if ($nullable->isAbsent()).getOrElse() for defaults (e.g., Nullable::from($user->address)->getOrElse(fn() => Address::default())).@var Nullable<Model>) to IDE-supported files.null checks → isAbsent()).Phase 2: Incremental Rollout
nullable: true).Phase 3: Full Adoption
Nullable with Laravel-specific helpers (e.g., Nullable::fromEloquent($model->relation)).Nullable usage via PHPStan or custom sniffs (e.g., "Disallow raw null in model attributes").try/catch.composer.json constraints). May require adjustments for older versions (e.g., PHP 8.0 features like named arguments).mbstring and json extensions are enabled for string/JSON operations.Nullable::from(json_decode($document['field'], true))).Nullable objects are serialized to JSON correctly (e.g., ->jsonSerialize()).| Priority | Component | Example Use Case | Risk |
|---|---|---|---|
| High | API Layer | Nullable request payloads, response fields | Breaking changes to contracts |
| High | Eloquent Models | Nullable relationships, computed attributes | Migration complexity |
| Medium | Services/Repositories | Business logic with optional dependencies | Refactoring existing methods |
| Low | CLI/Artisan Commands | Nullable config values | Minimal impact |
| Low | Tests | Mocking null scenarios | Requires test updates |
Order of Operations:
Nullable behavior.unwrap() failures in tests).Nullable::from($user->profile)->map(...) clearly expresses intent vs. if ($user->profile) { ... }.map/filter over loops).Nullable types (workHow can I help you explore Laravel packages today?