Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

[LaravelConf Taiwan 2019] 忘掉 foreach,從此學會 Collection!

126 views

Published on

LaravelConf Taiwan 2019 - 忘掉 foreach,從此學會 Collection!
鄭宇翔 - 跑跑腿科技 技術顧問

寫程式的時候,總免不了需要使用 for、foreach、while 等遞迴技巧來整理資料、調整陣列結構。但每次寫一層又一層的 foreach 時,總覺得可讀性太差,寫到最後連它媽都不認識它?

這場議程,宇翔就是要教你忘掉 foreach,從此改用 Collection。除了會從基礎介紹什麼是 Collection 外,還會比較 Support\Collection 和Eloquent\Collection 之間的差異,以及 Builder 和 Collection 同名的 method 的細微不同。並以 map、filter、reduce 三大常見手法,說明如何做資料處理、陣列轉型及 Functional Programing 的技巧。當然,官方文件上沒說的隱藏用法及各種踩過的坑也會一併帶到。

Published in: Technology
  • Be the first to comment

[LaravelConf Taiwan 2019] 忘掉 foreach,從此學會 Collection!

  1. 1. 忘掉 foreach 從此學會 Collection! LaravelConf Taiwan 2019
  2. 2. 自我介紹
  3. 3. 工商時間
  4. 4. 即時人力服務 達成您交辦的事 享受生活的好幫手 PAOPAOTUI COMPANY 跑跑腿 LINE ID:@85run
  5. 5. Hiring 天瀚國際科技 PHP Senior 後端工程師 60K - 100k * 14 hr@universetech.cc
  6. 6. Environment Version PHP 7.3.6 Laravel 5.8.28
  7. 7. What is Collection?
  8. 8. Create Collection 1. new 2. 3. 4.
  9. 9. Create Collection 1. new 2. static method 3. 4.
  10. 10. Create Collection 1. new 2. static method 3. helper function 4.
  11. 11. Create Collection 1. new 2. static method 3. helper function 4. wrap
  12. 12. Methods in Collection
  13. 13. Reference https://laravel.com/docs/master/collections#available-methods
  14. 14. Impletement ArrayAccess Countable IteratorAggregate JsonSerializable Arrayable Jsonable
  15. 15. Fluent API
  16. 16. collect()->take(10)->reverse()->toArray()
  17. 17. Before
  18. 18. IlluminateSupportCollecton vs IlluminateDatabaseEloquentCollection
  19. 19. When is EloquentCollection
  20. 20. $users = AppUser::all(); $orders = $user->orders
  21. 21. When is SupportCollection
  22. 22. collect();
  23. 23. Builder vs Collection
  24. 24. IlluminateDatabaseConcerns BuildsQueries
  25. 25. Functional Programming
  26. 26. Pointfree
  27. 27. Divide and Conquer
  28. 28. Pure Function
  29. 29. Immutable
  30. 30. $begin = Carbon::now(); $end = $begin->addHour();
  31. 31. Use FP in Collection
  32. 32. map、filter、reduce
  33. 33. Frequency of use
  34. 34. all() vs toArray()
  35. 35. average avg contains each every filter first flatMap groupBy keyBy map max min partition reject some sortBy sortByDesc sum unique when countBy
  36. 36. Higher order messages
  37. 37. Variant sum() sum(‘key’) sum(‘key.id’) sum(callback)
  38. 38. Extend Collection
  39. 39. Eloquent’s newCollection();
  40. 40. Warning
  41. 41. Type strict
  42. 42. Problem in PHP array
  43. 43. $collection->values() array_values()
  44. 44. Performance
  45. 45. Not use Laravel?
  46. 46. composer require illuminate/support
  47. 47. composer require tightenco/collect Reference https://github.com/tightenco/collect
  48. 48. Thanks for your participation

×