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

Data Model Laravel Package

zero-to-prod/data-model

Type-safe PHP data models that hydrate from arrays/JSON via a single from($data) call. Uses reflection, type hints, and #[Describe] attributes for defaults, required/nullable rules, casting, and assignment—ideal for APIs, DB rows, and user input.

View on GitHub
Deep Wiki
Context7

Zerotoprod\DataModel helps you build predictable, verifiable PHP data objects by hydrating typed properties from external inputs (APIs, DB rows, requests) with a single from($data) call. It uses reflection plus your type hints and #[Describe] attributes to keep mapping, casting, and validation colocated with the model—reducing boilerplate and drift.

  • One-call hydration via from($data) instead of manual array-to-property wiring
  • Attribute-driven rules with #[Describe] for casting/validation near the property
  • Type-safe mapping guided by PHP property types and predictable assignment
  • Null/default handling through required, nullable, and default semantics
  • Cleaner input boundaries by replacing repeated isset()/guard logic across code
Frequently asked questions about Data Model
How do I replace manual array-to-object hydration with `from($data)` in Laravel?
Replace `new User($request->all())` with `User::from($request->all())`. The `from()` method uses reflection and `#[Describe]` attributes to auto-map, cast, and validate properties from arrays or JSON, eliminating manual assignment and defensive checks.
Does this work with Laravel Eloquent models?
Yes, but it’s designed for DTOs or value objects. For Eloquent, use `from()` on a base model trait or extend the package to auto-hydrate from database arrays. Avoid mixing with Eloquent’s built-in hydration to prevent conflicts.
What Laravel versions and PHP requirements does this support?
Requires PHP 8.1+ for attributes and union types. Officially supports Laravel 9+ (PHP 8.0+ may need polyfills). Check the [GitHub actions](https://github.com/zero-to-prod/data-model/actions) for compatibility updates.
Can I use `#[Describe]` attributes with Laravel Validation?
Yes. Map `#[Describe]` rules (e.g., `required`, `nullable`, `cast`) to Laravel’s `Validator` by extending the package or using a custom rule resolver. Example: `#[Describe(required: true)]` becomes `required|string` in validation.
What’s the performance impact of reflection-based hydration?
Reflection adds ~10–20ms per hydration for complex objects. Benchmark in your API’s hot paths. Cache `ReflectionAttribute` instances for repeated hydrations (e.g., API responses) or use OPcache/JIT compilation.
How do I handle nested or recursive data structures?
Use the `via` parameter in `#[Describe]` to specify custom hydrators for nested objects or arrays. Example: `#[Describe(via: [Address::class])]` auto-hydrates an `Address` object from a nested array. Test edge cases like circular references.
Is this a drop-in replacement for `spatie/array-to-object`?
Yes, but with stricter typing and fewer edge cases. Replace `spatie/array-to-object` by adding the `DataModel` trait and using `from($data)`. It enforces PHP types and `#[Describe]` rules upfront, reducing runtime errors.
How do I handle API error responses when `from()` fails?
Catch `PropertyRequiredException` or `InvalidCastException` and return HTTP 422 with validation-like errors. Extend Laravel’s `Validator` to reuse `#[Describe]` rules or surface exceptions as JSON: `return response()->json(['errors' => $e->getErrors()], 422).`
Can I integrate this with Laravel’s `php artisan make:model`?
Not natively, but you can create a custom artisan command or IDE plugin. Example: A `make:dto` command could auto-generate `#[Describe]` attributes for new classes. Contribute to the package or fork to add this feature.
What’s the migration path for large Laravel codebases?
Start with non-critical DTOs (e.g., API responses, form requests). Phase 1: Add `DataModel` to new classes; keep legacy constructors. Phase 2: Refactor services to use `from()`. Phase 3: Deprecate old constructors via `@deprecated` PHPDoc. Use feature flags for gradual adoption.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport