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

Laravel Data Laravel Package

spatie/laravel-data

Create rich, typed data objects for Laravel that replace form requests and API transformers. Automatically map from requests, validate with inferred rules, transform to resources (with lazy/partial fields), and generate TypeScript definitions from the same source.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Unified Data Contracts: Eliminate redundancy in API resources, form requests, and validation logic by defining data structures once in a single Data class. Reduces technical debt and improves maintainability.
  • Type Safety: Enforce strict typing for frontend-backend communication via auto-generated TypeScript definitions, reducing runtime errors and improving developer experience.
  • Build vs. Buy: Buy—this package replaces manual DTOs, FormRequests, and API Resources, saving dev time while maintaining flexibility. Avoid reinventing wheel for data validation, serialization, and transformation.
  • Roadmap Priorities:
    • API-First Projects: Ideal for greenfield projects where data contracts are critical (e.g., microservices, public APIs).
    • Legacy Modernization: Gradually replace spaghetti validation logic in monolithic apps with typed Data objects.
    • Full-Stack Type Safety: Pair with Laravel Inertia/Vue/React for seamless type alignment between frontend and backend.
  • Use Cases:
    • API Development: Replace Laravel’s FormRequest + ApiResource combo with a single Data class.
    • Database Models: Store Data objects as Eloquent model attributes (e.g., User::profileData()).
    • Frontend Integration: Generate TypeScript interfaces from PHP Data classes for autocompletion and validation.
    • Validation Heavy Workflows: Reduce boilerplate for complex validation rules (e.g., conditional logic, nested objects).

When to Consider This Package

  • Adopt If:

    • Your team struggles with duplicated data definitions (e.g., same fields in FormRequests, API Resources, and TypeScript).
    • You need runtime type safety for API payloads (e.g., preventing null where string is expected).
    • Your stack includes Laravel + Inertia/Vue/React and you want to sync types between frontend/backend.
    • You’re building public APIs or microservices where data contracts are critical.
    • Your validation logic is complex (e.g., conditional rules, nested objects) and manual DTOs are error-prone.
  • Look Elsewhere If:

    • You’re using non-Laravel frameworks (e.g., Symfony, Django) or non-PHP backends.
    • Your project is small-scale with minimal data validation needs (e.g., CRUD apps with simple forms).
    • You cannot use PHP 8.1+ (package dropped PHP 8.1 support in v4.20.0).
    • Your team prefers manual control over data transformation (e.g., custom serialization logic not supported by Data).
    • You’re already using a mature alternative like:
      • API Platform (for hypermedia APIs).
      • Symfony’s Serializer + DTOs (if not Laravel-specific).
      • Custom DTO libraries with existing team buy-in.

How to Pitch It (Stakeholders)

For Executives:

"This package lets us write data contracts once—for APIs, forms, and frontend types—cutting development time by 30%+ and slashing bugs from inconsistent data handling. For example, instead of maintaining separate validation rules, API responses, and TypeScript interfaces for a UserProfile, we define it in a single PHP class. The package auto-generates TypeScript types, so our frontend team gets instant type safety without manual syncing. It’s a force multiplier for our devs, especially on API-heavy projects or full-stack apps using Inertia/Vue/React."

ROI:

  • Faster Development: Eliminate redundant code for validation, serialization, and frontend types.
  • Fewer Bugs: Type safety catches errors at runtime (e.g., wrong data shapes in API calls).
  • Scalability: Ideal for microservices or public APIs where data contracts are critical.

For Engineers:

*"Laravel Data replaces 3 separate artifacts (FormRequest, API Resource, TypeScript interface) with one typed PHP class. Key benefits:

  • Validation: Define rules once (e.g., #[Max(99.99)] public float $price)—no more scattered rules() methods.
  • API Responses: Serialize to JSON with ->toArray() or use as Laravel API Resources.
  • Frontend Types: Auto-generate TypeScript interfaces (e.g., SongData.ts) for Inertia/Vue/React.
  • Database Integration: Store Data objects as Eloquent model attributes.
  • Performance: Optimized for validation and transformation (e.g., lazy properties for partial payloads).

Example:

// Before: 3 files + manual sync
// 1. FormRequest.php (validation)
// 2. SongResource.php (API response)
// 3. song.d.ts (TypeScript)

// After: 1 file
class SongData extends Data {
    public function __construct(
        public string $title,
        #[Max(10)] public string $artist,
        public float $price,
    ) {}
}
// Auto-generates:
// - Validation rules (e.g., `artist` max length 10)
// - TypeScript interface for frontend
// - JSON serialization for APIs

When to Use:

  • New Laravel projects (especially API-first or full-stack).
  • Legacy apps with spaghetti validation logic.
  • Teams using Inertia/Vue/React and struggling with type sync.

Trade-offs:

  • Slight learning curve for Data class patterns.
  • Not a drop-in for frameworks outside Laravel.
  • Custom serialization logic may require workarounds.

Call to Action: ‘Let’s pilot this on our next API module or Inertia project. The dev team can start with one Data class and measure the reduction in boilerplate and bugs.’"*

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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
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