1. Sum to find the sum of prices
We will learnflatMap
map
sum
and other very practical Method
##
collect($arr)->pluck('order_products.*.price')->flatten(1)->sum();
2. Formatting the boarding gate
I hope to deepen my understanding ofmapand the application scenarios of
map
It is a mechanism to obtain data through API and convert the data into scores. We will first try the
foreachmethod, and then use Laravel's Collection to optimize and reconstructhttps: //api.github.com/users/...
##General solution:
##collection refactoring:
4. Convert comment format
Theimplodemethod in
Laravel Collectionis actually It is very useful in certain scenarios. Combined withmap, we can customize the format of each element with a high degree of freedom. This video is about converting the normal format to theMarkdown
format.
##5. Calculate monthly turnover
zipmethod to solve the problem of two arrays.
#6. Reduce to create lookup arrayIn Laravel Collection, there is another very common method:
, this is very useful when we create a lookup array, so that we can use Collection to reconstruct a complex array and turn it into a simple key value lookup array.
collect($employees)->pluck('email','name')
summethod and someCommon application scenarios of groupBy
Recommended related articles:
The above is the detailed content of Summary of usage of collection class in Laravel (code). For more information, please follow other related articles on the PHP Chinese website!