{"id":23003723,"url":"https://github.com/stechstudio/laravel-record","last_synced_at":"2025-10-11T12:33:54.956Z","repository":{"id":47608055,"uuid":"94149845","full_name":"stechstudio/laravel-record","owner":"stechstudio","description":"What if Laravel's Collection and Model classes had a baby?","archived":false,"fork":false,"pushed_at":"2024-03-12T23:31:12.000Z","size":34,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-28T16:55:18.305Z","etag":null,"topics":["collections","laravel","model"],"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/stechstudio.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":"2017-06-12T23:36:30.000Z","updated_at":"2023-09-06T12:02:34.000Z","dependencies_parsed_at":"2022-09-13T12:40:46.315Z","dependency_job_id":null,"html_url":"https://github.com/stechstudio/laravel-record","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stechstudio","download_url":"https://codeload.github.com/stechstudio/laravel-record/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229790943,"owners_count":18124608,"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":["collections","laravel","model"],"created_at":"2024-12-15T07:15:10.434Z","updated_at":"2025-10-11T12:33:49.921Z","avatar_url":"https://github.com/stechstudio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Record\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-record.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-record)\n[![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-record.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-record)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n\nI'm going to assume you already know all about Laravel's awesome [Collection](https://laravel.com/docs/5.4/collections) class, \nand hopefully you've read [Refactoring to Collections](https://adamwathan.me/refactoring-to-collections/). \n(If you haven't, stop what you're doing and go buy that PDF. You'll thank me later.)\n \nI also assume you know all about [Eloquent models](https://laravel.com/docs/5.4/eloquent). \n\nBut have you ever wanted some of the functionality of a model, merged onto a collection? \n\nThis is a super small, single-class library that brings those together just a bit.\n\n# Benefits\n\nStarting with the Collection class, I wanted to add:\n\n1) **Magic getter for attributes**. If you have key/value pairs in your collection, the Collection class does provide\nthe [get](https://laravel.com/docs/5.4/collections#method-get) method. But I'm lazy. And I like accessing my collection\nwith plain 'ol object notation. You know, like a model. Record lets you do `$collection-\u003eattribute`.\n\n2) **New collection for sub-arrays**. If you hand a multi-dimensional array to `collect()` and access a nested array, it's still\njust an array. Like `$collection-\u003eget('attribute')['subattribute']`. I want collections all the way down! This will turn \nany sub-array into a new instance of Record, allowing you to do `$record-\u003eattribute-\u003esubattribute-\u003eas-\u003edeep-\u003eas-\u003eyour-\u003earray-\u003egoes`. \nAnd because you still have a real collection at each level, you can use all of the goodies like `$record-\u003eattribute-\u003esubattribute-\u003ecount()`.\n\n3) **Custom accessors**. Just like Eloquent, you can extend the Record class and define a custom accessor. Create a\n`getFooAttribute()` method and then just use `$collection-\u003efoo` to get your custom computed attribute.\n\n# Quick example: handling rich arrays\n\nI find myself frequently needing to handle a multi-dimensional array, often a response from a remote web service.\nThis array may have attributes (like 'name' or 'id') as well as a nested collections (like 'data' or 'rows'). \n\nConsider this:\n\n```json\n{\n  \"name\" : \"My Blog\",\n  \"url\" : \"http://foo.dev\",\n  \"posts\" : [\n    {\n      \"id\" : 1,\n      \"title\" : \"Hello World\",\n      \"content\" : \"...\",\n      \"comments\" : [\n        {\n          \"name\" : \"John Doe\",\n          \"email\" : \"john@example.com\",\n          \"content\": \"...\"\n        }\n      ]\n    },\n    {\n      \"id\" : 2,\n      \"title\" : \"My second post\",\n      \"content\" : \"...\",\n      \"comments\" : [\n        ...\n      ]\n    }\n  ]\n}\n```\n\nWe can take this whole payload and navigate it quite nicely with Record:\n\n```php\n$record = record(json_decode($webServiceResponse, true));\n\necho $record-\u003ename; // My Blog\necho $record-\u003eposts-\u003ecount(); // 2\necho $record-\u003eposts-\u003efirst()-\u003etitle; // Hello World\necho $record-\u003eposts-\u003efirst()-\u003ecomments-\u003ecount(); // 1\n```\n\nNice! At each level I get a combination Laravel's Collection class, plus some attribute goodness borrowed from Model.\n\nFurthermore I might extend Record and create a class with custom accessors to sanitize `content`, or split the `name` into\nfirst and last, or... you get the idea.\n\n# Installation\n\nYou know the drill.\n\n```\ncomposer require stechstudio/laravel-record\n```\n\nThen you can either:\n\n```php\n$record = new STS\\Record\\Record([...]);\n```\n\nOr you can use the `record` helper method:\n\n```php\n$record = record([...]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstechstudio%2Flaravel-record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-record/lists"}