PHPackages                             timehunter/laravel-dto-generator - 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. timehunter/laravel-dto-generator

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

timehunter/laravel-dto-generator
================================

A generator that creates PHP Data Transfer Object by array schema.

2.2.1(6y ago)501772MITPHP

Since Jul 24Pushed 6y agoCompare

[ Source](https://github.com/RyanDaDeng/laravel-dto-generator)[ Packagist](https://packagist.org/packages/timehunter/laravel-dto-generator)[ Docs](https://github.com/timehunter/laravel-dto-generator)[ RSS](/packages/timehunter-laravel-dto-generator/feed)WikiDiscussions master Synced 2d ago

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

Laravel DTO Generator
=====================

[](#laravel-dto-generator)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a089d52a20a7d8400d0506cb07a695524c3e881f71680b737785ac0e4f91d58d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74696d6568756e7465722f6c61726176656c2d64746f2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timehunter/laravel-dto-generator)[![Total Downloads](https://camo.githubusercontent.com/81fa3d25910c85342cba18b0f339b0bdd634fb51653868abd9b5cab492f51f32/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74696d6568756e7465722f6c61726176656c2d64746f2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/timehunter/laravel-dto-generator)

A DTO generator that helps you create a bunch of models instead of repeating copy/paste.
----------------------------------------------------------------------------------------

[](#a-dto-generator-that-helps-you-create-a-bunch-of-models-instead-of-repeating-copypaste)

### Star me if you find its useful. ^.^

[](#star-me-if-you-find-its-useful-)

*Please update your version to be "~v2.0" and re-publish your config file*

Install the package in development dependencies:

Via Composer

```
composer require --dev timehunter/laravel-dto-generator "~2.2.1"
```

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

[](#installation)

1. Create your local config file

```
php artisan vendor:publish --provider="TimeHunter\LaravelDTOGenerator\LaravelDTOGeneratorServiceProvider"
```

2. Add your array schema in config
3. Run artisan command:

```
php artisan make:dto
```

4. Check your files under your specified file location

Example
-------

[](#example)

### Input

[](#input)

#### PHP Array Schema

[](#php-array-schema)

```
        'post'     => (object)[
            'author'    => (object)[
                'id'         => 1,
                'note'       => null,
                'rating'     => 4.6,
                'first_name' => '',
                'last_name'  => '',
            ],
            'comment'   => (object)[
                'comment_by' => (object)[
                    'is_active'  => false,
                    'first_name' => '',
                    'last_name'  => ''
                ],
                'content'    => ''
            ],
            'followers' => (object)[
                'id'             => 1,
                'follower_users' => (array)[
                    'first_name' => '',
                    'last_name'  => ''
                ]
            ],
            'views'     => 123,
            'text'      => '',
            'date'      => '2019-01-01'
        ],
        'feedback' => (object)[
            'comment' => ''
        ]
```

Note: each object should have key name defined.

- If it is an object please define it as a `object`, see the above.
- If your components are array of objects, please define it as a pure array.
- You have to assign a value for each property, for example, an empty string `''` for `first_name`.

#### Example Output Usage

[](#example-output-usage)

```
    $result = Followers::create()->setId(1)
            ->addFollowerUsers(
                FollowerUsers::create()
                    ->setFirstName('ss')
                    ->setLastName('dd')
            )
            ->addFollowerUsers(
                FollowerUsers::create()
                    ->setFirstName('ss')
                    ->setLastName('dd')
            );
        var_dump($result->toArray());
```

### Output - DTOs:

[](#output---dtos)

#### User Class

[](#user-class)

```
