在laravel架構下的有趣玩法
怎麼建立一個collecion?
非常的簡單,如下:
$collection = collect(['a', 'b', 'c', 'd', 'e']);
//完成建立一個collect
目前的可用function有哪些?
有非常多,像是輸出最大(小)值、平均值、條件值、篩選值、相同值、重複值、合併、總數....等
這篇挑選基礎且簡單的功能做介紹
Function基礎い介紹
all()
印出所有。
collect([1, 2, 3])->all();
// [1, 2, 3]
avg()
印出平均
$average = collect([1, 1, 2, 4])->avg();
// 2
combine()
兩個集合,合併成一個
$collection = collect(['name', 'age']);
$combined = $collection->combine(['George', 29]);
$combined->all();
// ['name' => 'George', 'age' => 29]
collect()
返回一個新的collection,也可以使用已經collect的資料
$collectionA = collect([1, 2, 3]);
$collectionB = $collectionA->collect();
$collectionB->all();
// [1, 2, 3]
concat()
增新一個數值或集合,到另一個集合的後面。(有點類似push)
$A = collect(['John Doe']);
$concatenated = $A->concat(['Jane Doe'])->concat(['name' => 'Johnny Doe']);
$concatenated->all();
// ['John Doe', 'Jane Doe', 'Johnny Doe']
contains()
檢查字串中是否有指定項目,並回傳True/false,如果使用containsStrict()
對比較會更嚴格
$collection = collect(['name' => 'Desk', 'price' => 100]);
$collection->contains('Desk');
// true
$collection->contains('New York');
// false
count()
計算項目總數
$collection = collect([1, 2, 3, 4]);
$collection->count();
// 4
countBy()
計算集合中每一個元素出現的次數
$collection = collect([1, 2, 2, 2, 3]);
$counted = $collection->countBy();
$counted->all();
// [1 => 1, 2 => 3, 3 => 1]
按照自己定義的條件進行groupby()
$collection = collect(['[email protected]', '[email protected]', '[email protected]']);
$counted = $collection->countBy(function ($email) {
return substr(strrchr($email, "@"), 1); //strrchr:擷取"@"後的所有字元,substr取從第1到最後的字
});
$counted->all();
// ['gmail.com' => 2, 'yahoo.com' => 1]
diff()
找出此集合與另一集合中不同之處,並返回此集合不存在的字元
$collection = collect([1, 2, 3, 4, 5]);
$diff = $collection->diff([2, 4, 5, 6, 8]);
$diff->all();
// [1, 3]
duplicates()
返回重複值的位址和重複值
$collection = collect(['a', 'b', 'a', 'c', 'b']);
$collection->duplicates();
// [2 => 'a', 4 => 'b']
except()
回傳除了指定的項目以外的所有值
$collection = collect(['product_id' => 1, 'price' => 100, 'discount' => false]);
$filtered = $collection->except(['price', 'discount']);
$filtered->all();
// ['product_id' => 1]
get()
給予指定鍵的項目,如果沒有則回傳null
$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
$value = $collection->get('name');
// taylor
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->all();
/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/
has()
回傳是否有指定鍵。hasAny()的條件較寬鬆,只要有其中一項有就回傳true
$collection = collect(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->has('product');
// true
$collection->has(['product', 'amount']);
// true
$collection->has(['amount', 'price']);
// false
isEmpty()
是否為空,也有另一種寫法:isNotEmpty()
collect([])->isEmpty();
// true
keys()
返回鍵值
$collection = collect([
'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]);
$keys = $collection->keys();
$keys->all();
// ['prod-100', 'prod-200']
nth()
印出每一個第n筆的內容
$collection = collect(['a', 'b', 'c', 'd', 'e', 'f']);
$collection->nth(4);
// ['a', 'e']
//第0筆,第4筆,第8筆.....
sort()
排序集合資料
$collection = collect([5, 3, 1, 2, 4]);
$sorted = $collection->sort();
$sorted->values()->all();
// [1, 2, 3, 4, 5]
sortby()
維給予一個鍵值,並依照給予的鍵值進行排序
$collection = collect([5, 3, 1, 2, 4]);
$sorted = $collection->sort();
$sorted->values()->all();
// [1, 2, 3, 4, 5]
toJson()
:轉成json格式
toArray()
:轉成數列格式
where()
類似於SQL語法的where,通過指定的鍵值回傳符合條件的集合
$collection = collect([
['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],
['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],
['name' => 'Sue', 'deleted_at' => null],
]);
$filtered = $collection->where('deleted_at', '!=', null);
$filtered->all();
/*
[
['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],
['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],
]
*/
進一步補充
filter()
filter 這個方法跟map有點相似,filter會將符合條件的值回傳,並且覆蓋至原本的collect中,而map則是回傳,並且建立一個新的collection。
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();// [3, 4]
map()
map會將符合條件的值回傳,並且建立一個新的collection
$collection = collect([1, 2, 3, 4, 5]);
$multiplied = $collection->map(function ($item, $key) {
return $item * 2;});
$multiplied->all();// [2, 4, 6, 8, 10]
transform()
transfrom會重複collecton中的值並且進行改變,改變結束後,取代原本的collection
$collection = collect([1, 2, 3, 4, 5]);
$collection->transform(function ($item, $key) {
return $item * 2;});
$collection->all();// [2, 4, 6, 8, 10]