Understanding Laravel Collections

Understanding Laravel Collections

A simple guide to familiarizing yourself with Laravel's collection methods.

The full video (including code examples) can be seen at https://www.youtube.com/watch?v=2aYlruGdo8g

Code can be viewed at https://github.com/tampabaylaravel/understanding-collections/tree/master/tests/Feature

Follow me on Twitter @GlennKimbleJr
https://twitter.com/GlennKimbleJr

2a72f9bc69f0d14c7e31de90f78eac65?s=128

Glenn Kimble Jr

July 16, 2019
Tweet

Transcript

  1. Understanding Laravel Collections with Glenn Kimble Jr

  2. What are collections?

  3. What are collections? Collections are objects designed to wrap around

    an array and expose the operations that we can perform on that array as methods on the collection. Adam Wathan
  4. What are collections? Collections are objects designed to wrap around

    an array and expose the operations that we can perform on that array as methods on the collection. Adam Wathan
  5. • array_reduce() ◦ Reduce an array of items down to

    a single value. • array_map() ◦ Transform data. PHP: Array Functions Allow you to interact with and manipulate arrays in various ways. • array_filter() ◦ filter out items you don’t want.
  6. Why use them?

  7. Why use them? 1. Method chaining. 2. Over 100 helper

    methods. 3. Eloquent already returns results to us as collections.
  8. Method Chaining

  9. Method Chaining

  10. Method Chaining

  11. Method Chaining: Helper Methods

  12. None
  13. Use collections in place of arrays

  14. Use collections in place of arrays

  15. Use collections in place of arrays

  16. Use collections in place of arrays Implement the following: •

    ArrayAccess ◦ Interface to provide accessing objects as arrays. • IteratorAggregate ◦ Interface to create an external Iterator. • Countable ◦ Allows us to use php’s count() function.
  17. Code Examples https://github.com/tampabaylaravel/understanding-collections/tree/master/tests/Feature