All Projects → stechstudio → laravel-record

stechstudio / laravel-record

Licence: MIT license
What if Laravel's Collection and Model classes had a baby?

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-record

ghini.desktop
plant collections manager (desktop version)
Stars: ✭ 23 (+9.52%)
Mutual labels:  collections
json-schema-js-gui-model
Handy gui model and associated translator that can be used to construct javascript UI forms from json-schemas
Stars: ✭ 19 (-9.52%)
Mutual labels:  model
blender-importer-unity
A tool to fix orientation issues from Blender to Unity
Stars: ✭ 23 (+9.52%)
Mutual labels:  model
FireSnapshot
A useful Firebase-Cloud-Firestore Wrapper with Codable.
Stars: ✭ 56 (+166.67%)
Mutual labels:  model
tensorflow-stack-ts
TensorFlow.js Full-Stack Starter Kit
Stars: ✭ 33 (+57.14%)
Mutual labels:  model
VBCorLib
The VBCorLib framework brings many of the powerful .NET classes to VB6.
Stars: ✭ 81 (+285.71%)
Mutual labels:  collections
mutable
State containers with dirty checking and more
Stars: ✭ 32 (+52.38%)
Mutual labels:  model
ethereum-economic-model
A modular dynamical-systems model of Ethereum's validator economics
Stars: ✭ 79 (+276.19%)
Mutual labels:  model
seapy
State Estimation and Analysis in Python
Stars: ✭ 25 (+19.05%)
Mutual labels:  model
php-underscore
PHP underscore inspired &/or cloned from _.js, with extra goodies like higher order messaging
Stars: ✭ 42 (+100%)
Mutual labels:  collections
go-streams
Stream Collections for Go. Inspired in Java 8 Streams and .NET Linq
Stars: ✭ 127 (+504.76%)
Mutual labels:  collections
sttp-model
Simple Scala HTTP model
Stars: ✭ 30 (+42.86%)
Mutual labels:  model
modelsafe
A type-safe data modelling library for TypeScript
Stars: ✭ 13 (-38.1%)
Mutual labels:  model
BMExport
一个 JSON 转 Objective-C,Swift class, Swift struct Model 属性的 Mac 小工具,【点击直接下载 https://github.com/liangdahong/BMExport/releases/download/2.0/BMExport2.0.dmg 】。
Stars: ✭ 22 (+4.76%)
Mutual labels:  model
Apollo
A basic Application with multiple functionalities built with FastAPI aim to help Users Buy New Items Provided using PaypalAPI 🚀
Stars: ✭ 22 (+4.76%)
Mutual labels:  model
PowerCollections
Powerfull Collections, Sets, Lists and Maps.
Stars: ✭ 15 (-28.57%)
Mutual labels:  collections
yii2-rest-client
REST client (AR-like model) for Yii Framework 2.0 (via yii2-http-client)
Stars: ✭ 19 (-9.52%)
Mutual labels:  model
Keywords-Abstract-TFIDF-TextRank4ZH
使用tf-idf, TextRank4ZH等不同方式从中文文本中提取关键字,从中文文本中提取摘要和关键词
Stars: ✭ 26 (+23.81%)
Mutual labels:  collections
Faker.Portable
C# faked data generation for testing and prototyping purpose.
Stars: ✭ 12 (-42.86%)
Mutual labels:  model
modelforge
Python library to share machine learning models easily and reliably.
Stars: ✭ 18 (-14.29%)
Mutual labels:  model

Laravel Record

Latest Version on Packagist Total Downloads Software License

I'm going to assume you already know all about Laravel's awesome Collection class, and hopefully you've read Refactoring to Collections. (If you haven't, stop what you're doing and go buy that PDF. You'll thank me later.)

I also assume you know all about Eloquent models.

But have you ever wanted some of the functionality of a model, merged onto a collection?

This is a super small, single-class library that brings those together just a bit.

Benefits

Starting with the Collection class, I wanted to add:

  1. Magic getter for attributes. If you have key/value pairs in your collection, the Collection class does provide the get method. But I'm lazy. And I like accessing my collection with plain 'ol object notation. You know, like a model. Record lets you do $collection->attribute.

  2. New collection for sub-arrays. If you hand a multi-dimensional array to collect() and access a nested array, it's still just an array. Like $collection->get('attribute')['subattribute']. I want collections all the way down! This will turn any sub-array into a new instance of Record, allowing you to do $record->attribute->subattribute->as->deep->as->your->array->goes. And because you still have a real collection at each level, you can use all of the goodies like $record->attribute->subattribute->count().

  3. Custom accessors. Just like Eloquent, you can extend the Record class and define a custom accessor. Create a getFooAttribute() method and then just use $collection->foo to get your custom computed attribute.

Quick example: handling rich arrays

I find myself frequently needing to handle a multi-dimensional array, often a response from a remote web service. This array may have attributes (like 'name' or 'id') as well as a nested collections (like 'data' or 'rows').

Consider this:

{
  "name" : "My Blog",
  "url" : "http://foo.dev",
  "posts" : [
    {
      "id" : 1,
      "title" : "Hello World",
      "content" : "...",
      "comments" : [
        {
          "name" : "John Doe",
          "email" : "[email protected]",
          "content": "..."
        }
      ]
    },
    {
      "id" : 2,
      "title" : "My second post",
      "content" : "...",
      "comments" : [
        ...
      ]
    }
  ]
}

We can take this whole payload and navigate it quite nicely with Record:

$record = record(json_decode($webServiceResponse, true));

echo $record->name; // My Blog
echo $record->posts->count(); // 2
echo $record->posts->first()->title; // Hello World
echo $record->posts->first()->comments->count(); // 1

Nice! At each level I get a combination Laravel's Collection class, plus some attribute goodness borrowed from Model.

Furthermore I might extend Record and create a class with custom accessors to sanitize content, or split the name into first and last, or... you get the idea.

Installation

You know the drill.

composer require stechstudio/laravel-record

Then you can either:

$record = new STS\Record\Record([...]);

Or you can use the record helper method:

$record = record([...]);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].