{"id":23980767,"url":"https://github.com/tailflow/dto","last_synced_at":"2025-07-13T05:35:33.445Z","repository":{"id":62550964,"uuid":"504811021","full_name":"tailflow/dto","owner":"tailflow","description":"A simple and lightweight implementation of Data Transfer Objects (DTO) in Laravel with optional casting support","archived":false,"fork":false,"pushed_at":"2022-12-27T18:06:31.000Z","size":28,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-18T15:47:28.410Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tailflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-18T10:23:57.000Z","updated_at":"2025-05-12T17:33:42.000Z","dependencies_parsed_at":"2023-01-31T05:01:06.918Z","dependency_job_id":null,"html_url":"https://github.com/tailflow/dto","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tailflow/dto","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fdto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fdto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fdto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fdto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tailflow","download_url":"https://codeload.github.com/tailflow/dto/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fdto/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265095945,"owners_count":23710756,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-07T10:18:35.820Z","updated_at":"2025-07-13T05:35:33.408Z","avatar_url":"https://github.com/tailflow.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Data Transfer Object\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/tailflow/dto.svg)](https://packagist.org/packages/tailflow/dto)\n[![Build Status on GitHub Actions](https://img.shields.io/github/actions/workflow/status/tailflow/dto/ci.yml?branch=main)](https://github.com/tailflow/dto/actions)\n\nA simple and lightweight implementation of Data Transfer Objects (DTO) in Laravel with optional casting support.\n\nUnder the hood it implements Laravel's [`Castable`](https://laravel.com/docs/8.x/eloquent-mutators#castables) interface with a Laravel [custom cast](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) that handles serializing between the `DataTransferObject` (or a compatible array) and your JSON database column.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require tailflow/dto\n```\n\n## Usage\n\n### 1. Create your `DataTransferObject` or `DataTransferObjectCollection`\n\n\n```php\nnamespace App\\DataTransferObjects;\n\nuse Tailflow\\DataTransferObjects\\DataTransferObject;\n\nclass Address extends DataTransferObject\n{\n    public string $country;\n    public string $city;\n    public string $street;\n}\n```\n\n```php\nnamespace App\\DataTransferObjects;\n\nuse Tailflow\\DataTransferObjects\\DataTransferObjectCollection;\n\nclass WorkAddresses extends DataTransferObjectCollection \n{\n    public static function getItemClass(): string\n    {\n        return Address::class;\n    }\n}\n```\n\n### (Optional) 2. Configure your Eloquent attribute casting:\n\nNote that this should be a `jsonb` or `json` column in your database schema.\n\n```php\nnamespace App\\Models;\n\nuse App\\DataTansferObjects\\Address;\nuse App\\DataTansferObjects\\WorkAddresses;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n    protected $casts = [\n        'address' =\u003e Address::class,\n        'work_addresses' =\u003e WorkAddresses::class,\n    ];\n}\n```\n\n### 3. Enjoy ~\n\nPass DTOs as arguments or use as return values, and get a nice autocompletion.\n\n```php\nfunction getAddress(Address $originalAddress): Address \n{\n    $address = new Address();\n    $address-\u003ecounty = $originalAddress-\u003ecountry;\n    $address-\u003ecity = 'Tokyo';\n    $address-\u003estreet = '4-2-8 Shiba-koen';\n  \n    return $address;\n}\n\n// or\n\nfunction getAddress(): Address \n{\n    return new Address(\n        [\n            'country' =\u003e 'Japan',\n            'city' =\u003e 'Tokyo',\n            'street' =\u003e '4-2-8 Shiba-koen',\n        ]\n    );\n}\n```\n\nOn Eloquent models, you can now pass either an instance of your `Address` class, or even just an array with a compatible structure. It will automatically be cast between your class and JSON for storage and the data will be validated on the way in and out.\n\n```php\n$workAddress = new Address();\n$workAddress-\u003ecountry = 'Japan';\n$workAddress-\u003ecity = 'Osaka';\n\n$user = User::create([\n    // ...\n    'address' =\u003e [\n        'country' =\u003e 'Japan',\n        'city' =\u003e 'Tokyo',\n        'street' =\u003e '4-2-8 Shiba-koen',\n    ],\n    'work_addresses' =\u003e [\n        $workAddress\n    ]\n\n]);\n\n$residents = User::where('address-\u003ecity', 'Tokyo')-\u003eget();\n```\n\nBut the best part is that you can decorate your class with domain-specific methods to turn it into a powerful value object.\n\n```php\n$user-\u003eaddress-\u003etoMapUrl();\n\n$user-\u003eaddress-\u003egetCoordinates();\n\n$user-\u003eaddress-\u003egetPostageCost($sender);\n\n$user-\u003eaddress-\u003ecalculateDistance($otherUser-\u003eaddress);\n\necho (string) $user-\u003eaddress;\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftailflow%2Fdto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftailflow%2Fdto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftailflow%2Fdto/lists"}