PHPackages                             bssphp/laraveldto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bssphp/laraveldto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bssphp/laraveldto
=================

A strongly typed Data Transfer Object integration for Laravel

1.7(3y ago)012MITPHPPHP ^8.0

Since Jan 14Pushed 3y ago1 watchersCompare

[ Source](https://github.com/bssphp/laraveldto)[ Packagist](https://packagist.org/packages/bssphp/laraveldto)[ RSS](/packages/bssphp-laraveldto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (7)Versions (9)Used By (0)

Laravel DTO
===========

[](#laravel-dto)

[![Latest Stable Version](https://camo.githubusercontent.com/9a8bd8daa31df0de06dce5aba4dc7975cfe92622f7636bb5ca7ff205e05b6584/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6273737068702f6c61726176656c64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/laraveldto)[![Total Downloads](https://camo.githubusercontent.com/c39a918ec31b75fc47d1c9813698f778848919b8881c7237ad6effd18b06998d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6273737068702f6c61726176656c64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/laraveldto)[![License](https://camo.githubusercontent.com/4fa2026681e21fea4b2355cfba88a0afc25cf40fb7a88246ffac4b9ec32117fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6273737068702f6c61726176656c64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/laraveldto)[![GitHub Build Status](https://camo.githubusercontent.com/f2f51caa8d77c67f8e21b6acfa78ef5c77d7826d8740c2950e1c436bf7ead7e2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6273737068702f6c61726176656c64746f2f54657374733f7374796c653d666c61742d737175617265)](https://github.com/bssphp/laraveldto/actions)

A strongly typed Data Transfer Object **for Laravel** without magic for PHP 8.0+

This package extends the functionality of [**bssphp/DTO**](https://github.com/bssphp/DTO) to provide more narrow usecases for Laravel applications.

Laravel-DTO serves as an **intermediate and reusable layer** between request input &amp; validation and model attribute population.

Contents
--------

[](#contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Validation](#validation)
    - [Populate models](#populate-models)
    - [Populate DTO from request](#populate-dto-from-request-input-data)
    - [**Combined usage**](#combined-usage)
- [Type casting](#type-casting)
- [Best practices](#best-practices)
- [Testing](#testing)

Installation
------------

[](#installation)

```
composer require bssphp/laraveldto

```

Usage
-----

[](#usage)

All data objects must extend the [`bssphp\laraveldto\AbstractModelData`](src/AbstractModelData.php) class.

### Validation

[](#validation)

When attaching the [`#[ValidationRule]`](src/Attributes/ValidationRule.php) any given data will be passed to the Laravel Validator so you can make use of all [available validation rules](https://laravel.com/docs/9.x/validation#available-validation-rules) and even built-in rules instances.

```
use App\Models\Person;
use App\Models\Project;
use Illuminate\Validation\Rules\Exists;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\ValidationRule;

class PersonData extends AbstractModelData
{
    #[ValidationRule(['required', 'string', 'min:1', 'max:255'])]
    public string $name;

    #[ModelAttribute(['sometimes', 'min:18'])]
    public int $currentAge;

    #[ValidationRule(['nullable', 'string', 'in:de,en'])]
    public ?string $language;

    #[ValidationRule(['required', 'numeric', new Exists(Project::class, 'id')])]
    public int $projectId;
}
```

This will throw a `Illuminate\Validation\ValidationException` if any rule does not pass.

```
$data = new PersonData([
    'name' => 'John Doe',
    'currentAge' => 25,
    'language' => 'de',
    'projectId' => 2,
]);
```

### Populate Models

[](#populate-models)

You can attach a model to any DTO using the [`#[ForModel(Model::class)]`](src/Attributes/ForModel.php) attribute. To associate DTO properties with Model attributes, you need to attach the [`#[ModelAttribute()]`](src/Attributes/ModelAttribute.php) attribute to each property. If no parameter is passed to the [`#[ModelAttribute]`](src/Attributes/ModelAttribute.php) attribute, DTO uses the property name itself.

```
use App\Models\Person;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\ModelAttribute;

#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
    #[ModelAttribute]                 // The `$name` DTO property will populate the `name` model attribute
    public string $name;

    #[ModelAttribute('current_age')]  // The `$currentAge` DTO property will populate the `current_age` model attribute
    public int $currentAge;

    public string $language;          // The `$language` DTO property will be ignored
}
```

**Create DTO and store to model**

```
$data = new PersonData([
    'name' => 'John Doe',
    'currentAge' => 25,
    'language' => 'de',
]);

$person = $data->toModel()->save();
```

**Attributes saved in `Person` model**

```
{
    "name": "John Doe",
    "current_age": 25
}
```

**Note**: You can also pass an existing model to the `toModel()` method.

```
use App\Models\Person;

$person = $data->toModel($person)->save();
```

**Note**: When passing **no** existing model to the `toModel()` method, default values declared in the DTO will be populated. If a model is passed as argument `toModel($model)` default values will not override existing model attributes.

### Populate DTO from request input data

[](#populate-dto-from-request-input-data)

When attaching the [`#[RequestAttribute]`](src/Attributes/RequestAttribute.php) and creating a DTO instance via the `fromRequest(Request $request)` method all matching attributes will be populated by the input data. If no parameter is passed to the [`#[RequestAttribute]`](src/Attributes/RequestAttribute.php) attribute, DTO uses the property name itself.

```
use App\Models\Person;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\ModelAttribute;
use bssphp\laraveldto\Attributes\RequestAttribute;

#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
    #[RequestAttribute]            // The `$name` DTO property will de populated by the `name` request attribute
    public string $name;

    #[RequestAttribute('my_age')]  // The `$currentAge` DTO property will be populated by `my_age` request attribute
    public int $currentAge;

    public string $language;       // The `$language` DTO property will not be populated
}
```

**The controller**

```
use App\Data\PersonData;
use Illuminate\Http\Request;

class TestController
{
    public function store(Request $request)
    {
        $data = PersonData::fromRequest($request);
    }
}
```

**Request input data**

```
{
  "name": "John Doe",
  "my_age": 25,
  "language": "de"
}
```

**The `PersonData` DTO instance**

```
App\Data\PersonData^ {
  +name: "John Doe"
  +currentAge: 25
}

```

### Combined usage

[](#combined-usage)

Of course all those attributes start to make sense if used together. You can attach all attributes separately of make use of the [`#[ValidatedRequestModelAttribute]`](src/Attributes/ValidatedRequestModelAttribute.php) attribute which combines the functionality of all [`#[RequestAttribute]`](src/Attributes/RequestAttribute.php), [`#[ModelAttribute]`](src/Attributes/ModelAttribute.php) and [`#[ValidationRule]`](src/Attributes/ValidationRule.php) attributes.

Both properties in the following example behave exactly the same. Use as you prefer.

```
use App\Models\Person;
use Illuminate\Validation\Rules\Exists;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\ModelAttribute;
use bssphp\laraveldto\Attributes\RequestAttribute;
use bssphp\laraveldto\Attributes\ValidatedRequestModelAttribute;
use bssphp\laraveldto\Attributes\ValidationRule;

#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
    // All attributes attached separately (looks disgusting doesn't it?)
    #[
        ValidationRule(['required', 'numeric', 'min:18']),
        RequestAttribute('my_age'),
        ModelAttribute('current_age')
    ]
    public string $currentAge;

    // Combined usage
    // The `my_age` request attribute will be validated and set to the `current_age` model attribute.
    #[ValidatedRequestModelAttribute(['required', 'numeric', 'min:18'], 'my_age', 'current_age')]
    public string $currentAge;
}
```

**Request input data**

```
{
  "my_age": 25
}
```

**The controller**

```
use App\Data\PersonData;
use Illuminate\Http\Request;

class TestController
{
    public function index(Request $request)
    {
        $person = PersonData::fromRequest($request)->toModel()->save();

        return $person->id;
    }
}
```

### Nested data

[](#nested-data)

In some cases you also want to create realted models with a single HTTP call. In this case you can make use of the [`#[NestedModelData(NestedData::class)]`](src/Attributes/NestedModelData.php) which will populate the DTO property with n instances of the defined DTO.

Note that we will not attach an [`#[ModelAttribute]`](src/Attributes/ModelAttribute.php) attribute to the `$address` DTO property since it should not be set to a model attribute.

All attributes attached to the nested DTO will just work as expected.

```
use App\Models\Person;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\NestedModelData;
use bssphp\laraveldto\Attributes\RequestAttribute;
use bssphp\laraveldto\Attributes\ValidatedRequestModelAttribute;
use bssphp\laraveldto\Attributes\ValidationRule;

#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
    #[ValidatedRequestModelAttribute(['required', 'string'])]
    public string $name;

    /**
     * @var AddressData[]
     */
    #[NestedModelData(AddressData::class), ValidationRule(['required', 'array']), RequestAttribute]
    public array $adresses;
}
```

```
use App\Models\Address;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ValidatedRequestModelAttribute;

#[ForModel(Address::class)]
class AddressData extends AbstractModelData
{
    #[ValidatedRequestModelAttribute(['string'])]
    public string $street;

    #[ValidatedRequestModelAttribute(['nullable', 'int'])]
    public ?int $apartment = null;
}
```

**Request input data**

```
{
  "name": "John Doe",
  "addresses": [
    {
      "street": "Sample Street"
    },
    {
      "street": "Debugging Alley",
      "apartment": 43
    }
  ]
}
```

**The controller**

```
use App\Data\PersonData;
use Illuminate\Http\Request;

class TestController
{
    public function index(Request $request)
    {
        $personData = PersonData::fromRequest($request);
        $person = $personData->toModel()->save();

        foreach ($personData->addresses as $addressData) {
            // We assume the `Person` model has a has-many relation with the `Address` model
            $person->addresses()->save(
                $addressData->toModel()
            );
        }

        return $person->id;
    }
}
```

Type Casting
------------

[](#type-casting)

Type casts will convert any given value to a specified type.

### Built-in type casts

[](#built-in-type-casts)

#### [`CastToDate`](src/Attributes/Casts/CastToDate.php)

[](#casttodate)

The [`#[CastToDate]`](src/Attributes/Casts/CastToDate.php) attribute will respect your customly defined date class from `Date::use(...)`. You can also specify a custom date class to be used by passing the date class name as single argument [`#[CastToDate(MyDateClass::class)]`](src/Attributes/Casts/CastToDate.php).

```
use Carbon\Carbon;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\Casts\CastToDate;

class PersonData extends AbstractModelData
{
    #[CastToDate]
    public Carbon $date;
}
```

### Custom type casts

[](#custom-type-casts)

You can declare custom type cast attributes by simply implementing the [`CastInterface`](src/Attributes/Casts/CastInterface.php) interface and attaching an attribute.

```
use Attribute;
use bssphp\laraveldto\Attributes\Casts\CastInterface;

#[Attribute]
class MyCast implements CastInterface
{
    public function castToType(mixed $value): mixed
    {
        return (string) $value;
    }
}
```

Best practices
--------------

[](#best-practices)

Make sure to add a `@method` PHPDoc comment like shown below to allow IDE and static analyzer support when calling the `toModel()` method.

```
use App\Models\Person;
use bssphp\laraveldto\AbstractModelData;
use bssphp\laraveldto\Attributes\ForModel;
use bssphp\laraveldto\Attributes\ModelAttribute;

/**
 * @method Person toModel()
 */
#[ForModel(Person::class)]
class PersonData extends AbstractModelData
{
    #[ModelAttribute]
    public string $name;
}
```

TODO
----

[](#todo)

- Allow array validation rules `field.*` &amp; Map into nested DTO
- Add correct validation exception error messages from nested fields
- Pass existing model to `toModel()` method
- Create DTO from existing model
- Only run validation rules if data provided from request

Testing
-------

[](#testing)

### PHPUnit

[](#phpunit)

```
./vendor/bin/phpunit

```

### PHPStan

[](#phpstan)

```
./vendor/bin/phpstan

```

Authors
-------

[](#authors)

- [Roman Zipp](https://github.com/bssphp)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

8

Last Release

1218d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6843e87aada702611e52851f3a751ed2150d054fbf4e9152b6ffc754fcf7a36a?d=identicon)[beanstacksys](/maintainers/beanstacksys)

---

Top Contributors

[![beanstacksys](https://avatars.githubusercontent.com/u/59993458?v=4)](https://github.com/beanstacksys "beanstacksys (4 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bssphp-laraveldto/health.svg)

```
[![Health](https://phpackages.com/badges/bssphp-laraveldto/health.svg)](https://phpackages.com/packages/bssphp-laraveldto)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
