惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Project Zero
Project Zero
N
Netflix TechBlog - Medium
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
The Register - Security
The Register - Security
P
Palo Alto Networks Blog
GbyAI
GbyAI
量子位
Simon Willison's Weblog
Simon Willison's Weblog
Cyberwarzone
Cyberwarzone
M
MIT News - Artificial intelligence
T
Threatpost
腾讯CDC
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
博客园 - 叶小钗
V
V2EX
美团技术团队
NISL@THU
NISL@THU
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
爱范儿
爱范儿
G
Google Developers Blog
博客园 - Franky
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
B
Blog
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Latest news
Latest news
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
F
Full Disclosure
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
Scott Helme
Scott Helme
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 数据收集原理分析 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 几种方法 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 check the export on the very top of this document so we can use $do mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 运维自动化之ANSIBLE | mafeifan 的编程技术分享 延伸 -- 关于前端路由 xdebug 3 和 2 的区别
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享

Laravel 集合是 Laravel 框架中一个十分有用的工具。

集合(Collection)

Illuminate\Support\Collection类了提供一个便捷的操作数组的封装。

集合 Collection 类实现了部分 PHP 和 Laravel 的接口,例如:

ArrayAccess- 用于操作数组对象的接口。 IteratorAggregate- 用于创建外部迭代器的接口。 JsonSerializable

创建一个新的集合

一个集合可以使用collect()帮助函数基于一个数组被创建或者直接通过Illuminate\Support\Collection类实例化。

一个非常简单的使用collect()帮助函数的示例:

$newCollection = collect([1, 2, 3, 4, 5]); dd($newCollection);

Eloquent ORM 集合

Eloquent ORM 的调用会以集合的形式返回数据

php

/**
 * 从用户表获取用户列表
 */
public function getUsers()
{
    $users = User::all();
    dd($users);
}

该控制器方法会返回一个如下显示的所有用户的 Laravel 集合。

你可以通过箭头符号便捷的访问集合属性。至于实例,想要获取 $users 集合的第一个用户的名字,我们可以这样做。

php

/**
*  获取第一个用户的名字
*/
public function firstUser()
{
   $user = User::first();
   dd($user->name);
}

实例1

有如下订单数组,要求按日期分组计算出总价

php

$orders = [
	[
		'id'    => 1,
		'price' => 9.8,
		'qty'   => 2,
		'date'  => '2018-10-10'
	],
	[
		'id'    => 2,
		'price' => 3.8,
		'qty'   => 1,
		'date'  => '2018-10-10'
	],
	[
		'id'    => 3,
		'price' => 5.0,
		'qty'   => 2,
		'date'  => '2018-10-11'
	]
];

期望结果:

php

 ["2018-10-10"]=> float(23.4) ["2018-10-11"]=> float(10)

过程:

php

$result = collect($orders)->groupBy('date')->map(function ($item) {
    return $item->sum(function ($item) {
                return $item['price'] * $item['qty'];
            }
        );
    }
);

dd($result->all());

实例2

使用tap调试集合 有时候我们希望在某集合处理过程中查看结果,这时可以使用tap

php

$items = [
    ['name' => 'David Charleston', 'member' => 1, 'active' => 1],
    ['name' => 'Blain Charleston', 'member' => 0, 'active' => 0],
    ['name' => 'Megan Tarash', 'member' => 1, 'active' => 1],
    ['name' => 'Jonathan Phaedrus', 'member' => 1, 'active' => 1],
    ['name' => 'Paul Jackson', 'member' => 0, 'active' => 1]
];

return collect($items)
    ->where('active', 1)
    ->tap(function($collection){
        // 输出  David Charleston, Megan Tarash, Jonathan Phaedrus, Paul Jackson 
        return var_dump($collection->pluck('name'));
    })
    ->where('member', 1)
    ->tap(function($collection){
        // 输出  David Charleston, Megan Tarash, Jonathan Phaedrus
        return var_dump($collection->pluck('name'));
    });

Tap vs Pipe

Laravel 也提供了另一个类似 tap 的集合操作方法 -- pipe,两者在集合调用上很类似,却有一个主要的区别:

通过调用 tap 方法不会改变原集合的结果,而 pipe 方法会根据返回值修改元集合的结果。示例如下:

php

return collect($items)
    ->where('active', 1)
    ->pipe(function ($collection) {
        return $collection->push(['name' => 'John Doe']);
    });
// David Charleston, Megan Tarash, Jonathan Phaedrus, Paul Jackson, John Doe

实例3

常用技巧 使用map添加新属性

php

$items = [
    ['name' => 'Finley Ma', 'age' => 18],
    ['name' => 'Jack Zhang', 'age' => 28],
];

$result = collect($items)
    ->map(function ($item, $key) {
       // 根据name追加一个firstName属性
       $item['firstName'] = explode(' ',$item['name'])[1];
       return $item;
    })
    // 指定数组的key为age,value为firstName
    ->pluck('firstName', 'age')
    ->all();

// [['18' => 'Ma'], ['28' => 'Zhang']]
dd($result);

参考