stechstudio/laravel-record
Composer 安装命令:
composer require stechstudio/laravel-record
包简介
What if Laravel's Collection and Model classes had a baby?
README 文档
README
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:
-
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. -
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(). -
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->footo 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" : "john@example.com",
"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([...]);
stechstudio/laravel-record 适用场景与选型建议
stechstudio/laravel-record 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.08k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2017 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stechstudio/laravel-record 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stechstudio/laravel-record 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-13