{"id":22123503,"url":"https://github.com/proai/laravel-struct","last_synced_at":"2026-04-01T17:02:13.468Z","repository":{"id":57044799,"uuid":"326987654","full_name":"ProAI/laravel-struct","owner":"ProAI","description":"💠 Struct implementation for Laravel including attribute casting.","archived":false,"fork":false,"pushed_at":"2025-05-29T15:40:53.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-27T23:42:21.287Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"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/ProAI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-01-05T12:10:28.000Z","updated_at":"2026-03-15T08:51:33.000Z","dependencies_parsed_at":"2024-08-28T00:48:58.966Z","dependency_job_id":null,"html_url":"https://github.com/ProAI/laravel-struct","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"7edcda4120868862155fac39f16f14dd492e7ccc"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/ProAI/laravel-struct","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Flaravel-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Flaravel-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Flaravel-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Flaravel-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ProAI","download_url":"https://codeload.github.com/ProAI/laravel-struct/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ProAI%2Flaravel-struct/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31290538,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T13:12:26.723Z","status":"ssl_error","status_checked_at":"2026-04-01T13:12:25.102Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":"2024-12-01T15:34:02.564Z","updated_at":"2026-04-01T17:02:13.443Z","avatar_url":"https://github.com/ProAI.png","language":"PHP","readme":"# Laravel Struct\n\n[![Latest Stable Version](https://poser.pugx.org/proai/laravel-struct/v/stable)](https://packagist.org/packages/proai/laravel-struct) [![Total Downloads](https://poser.pugx.org/proai/laravel-struct/downloads)](https://packagist.org/packages/proai/laravel-struct) [![Latest Unstable Version](https://poser.pugx.org/proai/laravel-struct/v/unstable)](https://packagist.org/packages/proai/laravel-struct) [![License](https://poser.pugx.org/proai/laravel-struct/license)](https://packagist.org/packages/proai/laravel-struct)\n\nA struct is a collection of typed variables. Structs are a well known datatype in other programming languages, but unfortunately not natively part of PHP yet. This package aims to bring structs to PHP and in particular to Laravel.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require proai/laravel-struct\n```\n\nPlease note that you need at least **PHP 7.4** and **Laravel 8** for this package.\n\n## Usage\n\nThe package uses named properties, which were introduced in PHP 7.4, to define a struct:\n\n```php\nuse App\\Structs\\GeoLocation;\nuse ProAI\\Struct\\Struct;\n\nclass Address extends Struct\n{\n    public string $street;\n\n    public string $city;\n\n    public GeoLocation $geo_location;\n}\n```\n\nYou can use all primitive types like `string`, `bool`, `float`, `int`, but also you can type a property as an object. The object can also be another struct, which enables you to nest structs (like `GeoLocation` above).\n\nStructs are instantiated by using an array of values:\n\n```php\n$address = new Address([\n    'street' =\u003e 'Baker Street',\n    'city' =\u003e 'London',\n    'geo_location' =\u003e [\n        'latitude' =\u003e 51.52,\n        'longitude' =\u003e -0.1566,\n    ],\n]);\n```\n\nProperties that are typed as objects will be converted to these objects on instantiation. Thus in the example above an object of `App\\Structs\\GeoLocation` will be created for the `$geo_location` property.\n\nProperties can be accessed normally:\n\n```php\n$address-\u003estreet\n=\u003e \"Baker Street\"\n\n$address-\u003ecountry\n=\u003e App\\Structs\\GeoLocation { ... }\n```\n\n_Hint: Snake cased properties are used to mimic the behaviour of Eloquent attributes._\n\n### Attribute Casting\n\nYou can use attribute casting with structs in your Eloquent models:\n\n```php\nuse App\\Structs\\Address;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n    protected $casts = [\n        'address' =\u003e Address::class,\n    ];\n}\n```\n\nIn order to make this work you need to define a `json` column of the specified key, so in this case `address`.\n\nAlternatively you can use the composed struct caster by adding the argument `composed` to compose a struct from multiple columns:\n\n```php\nuse App\\Structs\\Address;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model\n{\n    protected $casts = [\n        'address' =\u003e Address::class.':composed',\n    ];\n}\n```\n\nThe column names must start with the specified key followed by an underscore and the property name. This also works with nested structs. For the example above we would need the following columns:\n\n```\naddress_street\naddress_city\naddress_geo_location_latitude\naddress_geo_location_longitude\n```\n\nFinally you can also write your own custom caster by overwriting the `castUsing` method of the struct like described in the Laravel docs.\n\n### Collections\n\nSometimes you need an array of structs. For this purpose you can define a struct collection. The struct collection class is inherited from the Laravel collection class, so you can use all methods of a Laravel collection.\n\n```php\nuse App\\Structs\\Address;\nuse ProAI\\Struct\\Collection;\n\nclass AddressCollection extends Collection\n{\n    public $type = Address::class;\n}\n```\n\nBy the way, attribute casting also works with struct collections for `json` columns.\n\n## Support\n\nBugs and feature requests are tracked on [GitHub](https://github.com/proai/laravel-struct/issues).\n\n## License\n\nThis package is released under the [MIT License](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproai%2Flaravel-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fproai%2Flaravel-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproai%2Flaravel-struct/lists"}