What is the function of collection in laravel
Today, I will talk to you about the role of collection in laravel. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Pluck
Public function getDeductCourse () {return TeacherFinanceLogModel::select (['order_id',' period'])-> where ('type', 6)-> get ()-> pluck (' period', 'order_id')-> all ();}
Group map keyBy
Public function getStudentCourseByUids ($uids) {$studentCourse = $this- > studentCourseModel-> select (['uid',' grade_type', 'subject',' period', 'old_period',' period_total', 'finish_period',' wait_period', 'total_free_period',' free_period', 'wait_free_period'])-> whereIn (' uid', $uids)-> get () Return collect ($studentCourse)-> groupBy ('uid')-> map (function ($item) {return $item- > keyBy ($value) {return sprintf ("% s-%s-%s", $value [' uid'], $value ['grade_type'], $value [' subject']);})-> toArray ();})-> toArray ();}
Filter
Public function getRewardsAndPunishByUid ($uids) {$rewardPunish = $this- > courseRewardsModel-> select (["course.uid", "course.grade_type", "course.subject", "course.course_type", "course.period_type", "course.course_id" "course.tid", "course_rewards.s_rewards", "course_rewards.t_rewards", "course_rewards.s_option_status", "course_rewards.t_option_status")-> leftjoin ("course", "course.course_id", "=" "course_rewards.course_id")-> whereIn ('course.uid', $uids)-> where ("course.course_type", 1)-> get () Return collect ($rewardPunish)-> filter (function ($item) {return ($item- > s_rewards = = 1 | | $item- > s_rewards = =-1) & & $item- > s_option_status! = 1;})-> groupBy ('uid')-> map (function ($item) {return [' reward_total' = > $item- > filter (function ($value) {return $value- > s_rewards = 1 })-> sum ('sworn rewarded'), 'punish_total' = > abs ($item- > filter (function ($value) {return $value- > s_rewards = =-1;})-> sum (' sworn rewarded')];})-> toArray () } after reading the above, do you have any further understanding of the role of collection in laravel? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.