PHPackages                             lemax10/dto-helpers - 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. lemax10/dto-helpers

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

lemax10/dto-helpers
===================

Laravel DTO Helpers

v1.4.1(1y ago)6702MITPHPPHP ^8.0.0

Since Oct 19Pushed 1y ago1 watchersCompare

[ Source](https://github.com/LeMaX10/dto-helpers)[ Packagist](https://packagist.org/packages/lemax10/dto-helpers)[ Docs](https://pyankov.pro)[ RSS](/packages/lemax10-dto-helpers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (11)Used By (0)

PHP Laravel DTO Helpers
=======================

[](#php-laravel-dto-helpers)

This package offers a set of traits to simplify working with DTOs (Data Transfer Objects) in Laravel. It allows you to:

- Convert a DTO to an array using the `AsArray` trait.
- Transform a DTO into a JSON string via the `AsJson` trait.
- Seamlessly serialize a DTO with `json_encode()` using the `AsJsonSerialize` trait.
- Clone a DTO with modified properties through the `AsCloneable` trait.
- Make a DTO with static method `make()`, using `AsMake` trait
- Use DTOs as custom casts in Eloquent models.
- Use Array helpers `only()` or `except()` from DTO with using `AsArray` trait.

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

[](#installation)

Install the library using Composer:

```
composer require lemax10/dto-helpers
```

### AsArray

[](#asarray)

Converts your DTO to an array.
*Requires:* Implements `Illuminate\Contracts\Support\Arrayable`.

```
use LeMaX10\DtoHelpers\Traits\AsArray;
use Illuminate\Contracts\Support\Arrayable;

class MyData implements Arrayable
{
    use AsArray;

    public function __construct(
        public string $key,
        public string $value
    ) {}
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']

dump($dto->only('key'));
// Output: ['key' => 'test1']

dump($dto->except('key'));
// Output: ['value' => 'value1']
```

---

### AsJson

[](#asjson)

Converts your DTO to a JSON string.
*Requires:* Implements `Illuminate\Contracts\Support\Jsonable`.

```
use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;

class MyData implements Jsonable
{
    use AsJson;
    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'
```

---

### AsJsonSerialize

[](#asjsonserialize)

Enables JSON serialization with `json_encode()`.
*Requires:* Implements `JsonSerializable`.

```
use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;

class MyData implements JsonSerializable
{
    use AsJsonSerialize;

    // ...
}

$dto = MyData::make(key: 'test1', value: 'value1');
dump(json_encode($dto));
// Output: {"key":"test1","value":"value1"}
```

---

### AsMake

[](#asmake)

Enables JSON serialization with `json_encode()`.
*Requires:* Implements `LeMaX10\DtoHelpers\Contracts\Makeable`.

```
use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;

class MyData implements Makeable
{
    use AsMake;

    // ...
}

class MyDataCustomMake implements Makeable
{
    // ...
    public static function make(...$arguments): static
    {
        // Example so-so
        if (Arr::get($arguments, 'key') === 1) {
            $arguments['value'] = 'valueExample';
        }

        return new static(...$arguments);
    }
}

$dto = MyData::make(key: 'test1', value: 'value1');
dump($dto);
// Output: instance of MyData

$dtoCustom = MyDataCustomMake::make(key: 1, value: 'value1');
dump($dto);
// Output: instance of MyDataCustomMake and value equals "valueExample"
```

---

### AsCloneable

[](#ascloneable)

Clones a DTO while allowing property overrides.
*Requires:* Implements `LeMaX10\DtoHelpers\Contracts\Cloneable`

```
use LeMaX10\DtoHelpers\Traits\AsArray;
use LeMaX10\DtoHelpers\Traits\AsCloneable;
use LeMaX10\DtoHelpers\Contracts\Cloneable;
use Illuminate\Contracts\Support\Arrayable;

class MyData implements Cloneable, Arrayable
{
    use AsCloneable, AsArray;

    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']

$clone = $dto->clone(['key' => 'test2']);

dump($clone->toArray());
// Output: ['key' => 'test2', 'value' => 'value1']
```

---

### AsMake

[](#asmake-1)

Converts your DTO to a JSON string.
*Requires:* Implements `Illuminate\Contracts\Support\Jsonable`.

```
use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;

class MyData implements Jsonable
{
    use AsJson;
    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'
```

---

### Eloquent Model Casts

[](#eloquent-model-casts)

Use your DTO as a custom cast in an Eloquent model.

```
use LeMaX10\DtoHelpers\Casts\AsDto;
use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    protected $casts = [
        'dto' => AsDto::class . ':' . MyData::class,
    ];

    // OR

    public function casts()
    {
        return [
           'dto' => AsDto::cast(MyData::class),
        ];
    }
}

$model = ExampleModel::find(1);

dump($model->dto);
// Output: instance of MyData
```

Contributing
------------

[](#contributing)

Thank you for considering contributing to the DTO Helpers package!

License
-------

[](#license)

The DTO Helpers package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance48

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 58.3% 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 ~61 days

Recently: every ~16 days

Total

10

Last Release

384d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ef45edc1714e9db2e22281c81519aacedecf52235e89e3c5a2434a12b9b3f87c?d=identicon)[LeMaX10](/maintainers/LeMaX10)

---

Top Contributors

[![LeMaX10](https://avatars.githubusercontent.com/u/10564391?v=4)](https://github.com/LeMaX10 "LeMaX10 (7 commits)")[![tabuna](https://avatars.githubusercontent.com/u/5102591?v=4)](https://github.com/tabuna "tabuna (5 commits)")

---

Tags

phplaravelhelpersdto

### Embed Badge

![Health badge](/badges/lemax10-dto-helpers/health.svg)

```
[![Health](https://phpackages.com/badges/lemax10-dto-helpers/health.svg)](https://phpackages.com/packages/lemax10-dto-helpers)
```

###  Alternatives

[gehrisandro/tailwind-merge-laravel

TailwindMerge for Laravel merges multiple Tailwind CSS classes by automatically resolving conflicts between them

341682.2k18](/packages/gehrisandro-tailwind-merge-laravel)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)[transprime-research/piper

PHP Pipe method execution with values from chained method executions

174.6k2](/packages/transprime-research-piper)

PHPackages © 2026

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