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
Valueobjects

Valueobjects Laravel Package

cubicmushroom/valueobjects

A small PHP package providing lightweight Value Object classes to model immutable domain values (e.g., IDs, money, email, dates) with validation and type-safety. Useful for cleaner Laravel apps and DDD-style codebases.

View on GitHub
Deep Wiki
Context7

Clone of nicolopignatelli/valueobjects which has been removed

Frequently asked questions about Valueobjects
How do I install cubicmushroom/valueobjects in a Laravel project?
Run `composer require cubicmushroom/valueobjects` to install. No Laravel-specific dependencies exist, so it integrates purely via PHP. Use Laravel’s service container with `bind()` to resolve custom value objects, like `$this->app->bind(Money::class, fn() => new Money(100, 'USD'))`.
Does this package support Laravel Eloquent models?
Yes, but manually. Store value objects as JSON in database columns or split them into scalar fields (e.g., `money_amount` and `money_currency`). Use Eloquent accessors/mutators to serialize/deserialize objects. For complex types, consider Doctrine DBAL or custom model observers.
What Laravel versions does cubicmushroom/valueobjects support?
The package has no Laravel dependencies, but it’s optimized for **PHP 8+** and works best with **Laravel 9+** for modern type hints and service container features. Test thoroughly in your target Laravel version, as some edge cases may require adjustments.
Can I use this for API request validation in Laravel?
Absolutely. Use value objects in `FormRequest` classes for type-safe validation. For example, validate an `Email` value object in `validate()` or use API resources to return value objects as structured responses. This enforces domain rules early in the request lifecycle.
How do I handle database serialization for value objects?
For simple types (e.g., `Email`), store as a string column. For complex types (e.g., `Money`), use JSON columns or split into multiple fields. Implement `JsonSerializable` or custom accessors/mutators in Eloquent models. Avoid bloating database queries with deep object graphs.
Are there performance concerns with value objects in high-traffic APIs?
Serialization/deserialization of nested value objects can add overhead. Mitigate by caching frequently used objects (e.g., `Currency` enums) or lazy-loading complex types. Benchmark critical paths, especially in APIs with high request volumes, to identify bottlenecks.
What alternatives exist to cubicmushroom/valueobjects for Laravel?
Consider **spatie/value-object** (Laravel-aware with Eloquent traits) or **php-value-object** (more opinionated). For DDD-heavy apps, evaluate **ddd-php/value-object** or **league/value-object**. Each varies in features like database integration or immutability enforcement.
How do I test value objects in Laravel?
Unit test validation logic with PHPUnit (e.g., `assertTrue(new Email('test@example.com')->isValid())`). Test serialization by mocking Eloquent models or API responses. For integration, verify value objects persist correctly in database migrations or API payloads.
Is cubicmushroom/valueobjects actively maintained?
The package is a fork of an abandoned repo (**nicolopignatelli/valueobjects**), so monitor for updates or forks. Check GitHub issues for open problems. For critical projects, consider contributing or maintaining a private fork to ensure long-term support.
How do I create custom value objects for my domain?
Extend the base `ValueObject` class and define invariants in the constructor (e.g., `if (!$this->isValidEmail()) throw new InvalidArgumentException()`). Use type hints for constructor parameters (e.g., `public function __construct(private string $value)`). Override `equals()` for custom comparison logic.
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.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon