常用的有
count()
count
方法返回集合中所有項的數目:
$collection = collect([1, 2, 3, 4]); $collection->count(); // 4
forPage()
forPage
方法返回新的包含給定頁數數據項的集合:
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9])->forPage(2, 3); $collection->all(); // [4, 5, 6]
map()
map
方法遍歷集合並傳遞每個值給給定回調。該回調可以修改數據項並返回,從而生成一個新的經過修改的集合:
1 $collection = collect([1, 2, 3, 4, 5]); 2 3 $multiplied = $collection->map(function ($item, $key) { 4 return $item * 2; 5 }); 6 7 $multiplied->all(); 8 // [2, 4, 6, 8, 10]
groupBy()
groupBy
方法通過給定鍵分組集合數據項:
$collection = collect([ ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x10', 'product' => 'Bookcase'], ['account_id' => 'account-x11', 'product' => 'Desk'], ]); $grouped = $collection->groupBy('account_id'); $grouped->toArray(); /* [ 'account-x10' => [ ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x10', 'product' => 'Bookcase'], ], 'account-x11' => [ ['account_id' => 'account-x11', 'product' => 'Desk'], ], ] */
filter()
filter
方法通過給定回調過濾集合,只有通過給定測試的數據項才會保留下來:
$collection = collect([1, 2, 3, 4]); $filtered = $collection->filter(function ($item) { return $item > 2; }); $filtered->all(); // [3, 4]
collapse()
collapse
方法將一個多維數組集合收縮成一個一維數組:
$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); $collapsed = $collection->collapse(); $collapsed->all(); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
flatten()
flatten
方法將多維度的集合變成一維的:
$collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]); $flattened = $collection->flatten(); $flattened->all(); // ['taylor', 'php', 'javascript'];
注意:collapse() 與 flatten() 效果是不相同的;
last()
last
方法返回通過測試的集合的最后一個元素:
collect([1, 2, 3, 4])->last(function ($key, $value) { return $value < 3; }); // 2
merge()
merge
方法合並給定數組到集合。該數組中的任何字符串鍵匹配集合中的字符串鍵的將會重寫集合中的值:
$collection = collect(['product_id' => 1, 'name' => 'Desk']); $merged = $collection->merge(['price' => 100, 'discount' => false]); $merged->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]
pluck()
pluck
方法為給定鍵獲取所有集合值:
$collection = collect([ ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ]); $plucked = $collection->pluck('name'); $plucked->all(); // ['Desk', 'Chair']
search()
search
方法為給定值查詢集合,如果找到的話返回對應的鍵,如果沒找到,則返回false
:
$collection = collect([2, 4, 6, 8]); $collection->search(4); // 1
上面的搜索使用的是松散比較,要使用嚴格比較,傳遞true
作為第二個參數到該方法:
$collection->search('4', true); // false
sum()
sum
方法返回集合中所有數據項的和:
collect([1, 2, 3, 4, 5])->sum(); // 15
toArray()
toArray
方法將集合轉化為一個原生的PHP數組。如果集合的值是Eloquent模型,該模型也會被轉化為數組:
$collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toArray(); /* [ ['name' => 'Desk', 'price' => 200], ] */
注意:toArray
還將所有嵌套對象轉化為數組。如果你想要獲取底層數組,使用all
方法。
toJson()
toJson
方法將集合轉化為JSON:
$collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toJson(); // '{"name":"Desk","price":200}'
unique()
unique
方法返回集合中所有的唯一數據項:
$collection = collect([1, 1, 2, 2, 3, 4, 2]); $unique = $collection->unique(); $unique->values()->all(); // [1, 2, 3, 4]
可查詢資料 http://laravelacademy.org/post/178.html#toc_10
【Tag: collection】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_3
【 Laravel 5.1 文檔 ] 服務 —— 集合】http://laravelacademy.org/post/178.html#ipt_kb_toc_178_49
【 Laravel 5.4 文檔 ] 綜合話題 —— 集合】http://laravelacademy.org/post/6863.html