spatie/laravel-data

Powerful, type-safe data transfer objects and API resources for Laravel

Library
Composer
v4.23.0
1,782stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
65/100Good
Development Activity36
Maintenance56
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture83
Code Quality84
Innovation80
Learning Curve72

laravel-data lets Laravel developers define strongly-typed data transfer objects (DTOs) once and use them everywhere: as validated request input, as API resources for responses, and as typed value objects passed around application code. Instead of maintaining separate form requests, API resources, and plain arrays for the same shape of data, a single Data class handles validation, transformation, and serialization.

The library reads PHP type hints and docblocks to automatically infer validation rules, generates TypeScript definitions for frontend consumption, and supports advanced features like lazy properties, nested/collection DTOs, and cursor/offset pagination wrappers — all while staying idiomatic to how Laravel already works with requests and resources.

What You Get

  • Data base class for defining typed DTOs with automatic validation rule inference from PHP types
  • Automatic request-to-DTO hydration and DTO-to-JSON/array transformation for API resources
  • Lazy properties to conditionally include expensive-to-compute fields only when needed
  • DataCollection, PaginatedDataCollection, and CursorPaginatedDataCollection for typed collections and pagination
  • TypeScript type generation so frontend code shares the same data shape as the backend
  • Casts, transformers, and normalizers for customizing how values move in and out of a DTO

Common Use Cases

  • Replacing Laravel form requests and API resources with a single validated, typed DTO class
  • Sharing consistent typed shapes between backend Data classes and frontend TypeScript via generated types
  • Building paginated JSON APIs with typed, transformable response objects
  • Passing structured, type-checked data between application layers (controllers, jobs, services) instead of raw arrays

Under The Hood

Architecture - The package centers on src/Data.php as the base DTO class, composed with a pipeline architecture (DataPipeline.php, DataPipes/) that runs incoming data through configurable stages — normalization (Normalizers/), casting (Casts/), validation, and transformation (Transformers/) — before producing a hydrated object or serialized output. Resolvers/ and Mappers/ handle property name mapping and type resolution, while RuleInferrers/ inspects PHP type hints/docblocks to auto-generate Laravel validation rules without the developer writing them by hand.

Tech Stack - PHP 8.1+, built as a standard Laravel package via spatie/laravel-package-tools, using phpdocumentor/reflection-docblock and type-resolver to parse docblocks for type/validation inference, and spatie/php-structure-discoverer for auto-discovering Data classes in a project.

Code Quality - The repo has an extensive tests/ directory (171+ test files) covering casts, transformers, pipes, and edge cases, uses Pest as its test framework, PHPStan for static analysis (with a maintained baseline file), and PHP-CS-Fixer for style enforcement — a well-instrumented setup for a package with this much surface area (attributes, casts, normalizers, resolvers all interacting).

API Design - The core workflow — extend Data, type your public properties, done — requires almost no boilerplate for the common case, while advanced behavior (lazy loading, custom casts, TypeScript generation, pagination wrappers) is opt-in via attributes or interface implementation, keeping the entry-level API approachable despite the package’s considerable depth.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search