{"id":23980771,"url":"https://github.com/tailflow/castable-dto","last_synced_at":"2025-04-14T04:34:00.885Z","repository":{"id":57064574,"uuid":"311399884","full_name":"tailflow/castable-dto","owner":"tailflow","description":"Automatically cast JSON columns to PHP objects in Laravel","archived":false,"fork":false,"pushed_at":"2022-12-19T16:07:08.000Z","size":28,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T22:41:03.763Z","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":"2020-11-09T16:33:18.000Z","updated_at":"2024-09-16T11:53:04.000Z","dependencies_parsed_at":"2023-01-29T22:15:16.610Z","dependency_job_id":null,"html_url":"https://github.com/tailflow/castable-dto","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fcastable-dto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fcastable-dto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fcastable-dto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tailflow%2Fcastable-dto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tailflow","download_url":"https://codeload.github.com/tailflow/castable-dto/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248822865,"owners_count":21167128,"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.935Z","updated_at":"2025-04-14T04:34:00.861Z","avatar_url":"https://github.com/tailflow.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Castable Data Transfer Object\n\n### Deprecated\nPlease consider using [tailflow/dto](https://github.com/tailflow/dto) instead.\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/tailflow/castable-dto.svg)](https://packagist.org/packages/tailflow/castable-dto)\n[![Build Status on GitHub Actions](https://img.shields.io/github/actions/workflow/status/tailflow/castable-dto/ci.yml?branch=main)](https://github.com/tailflow/castable-dto/actions)\n\nHave you ever wanted to cast your JSON columns to a value object?\n\nThis package gives you an extended version of Spatie's `DataTransferObject` class, called `CastableDataTransferObject`.\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/castable-dto\n```\n\n## Usage\n\n### 1. Create your `CastableDataTransferObject`\n\nCheck out the readme for Spatie's [data transfer object](https://github.com/spatie/data-transfer-object) package to find out more about what their `DataTransferObject` class can do.\n\n``` php\nnamespace App\\DataTansferObjects;\n\nuse Tailflow\\DataTransferObjects\\CastableDataTransferObject;\n\nclass Address extends CastableDataTransferObject\n{\n    public string $country;\n    public string $city;\n    public string $street;\n}\n```\n\n### 2. Configure your Eloquent attribute to cast to it:\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 Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n    protected $casts = [\n        'address' =\u003e Address::class,\n    ];\n}\n```\n\nAnd that's it! 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$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]);\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## Credits\n\n- Original [package](https://github.com/jessarcher/laravel-castable-data-transfer-object) by [Jess Archer](https://github.com/jessarcher)\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%2Fcastable-dto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftailflow%2Fcastable-dto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftailflow%2Fcastable-dto/lists"}