Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Glenn Kimble Jr

July 16, 2019
Tweet

Other Decks in Programming

Transcript

  1. Understanding
    Laravel Collections
    with Glenn Kimble Jr

    View Slide

  2. What are collections?

    View Slide

  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

    View Slide

  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

    View Slide

  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.

    View Slide

  6. Why use them?

    View Slide

  7. Why use them?
    1. Method chaining.
    2. Over 100 helper methods.
    3. Eloquent already returns results to us as collections.

    View Slide

  8. Method Chaining

    View Slide

  9. Method Chaining

    View Slide

  10. Method Chaining

    View Slide

  11. Method Chaining: Helper Methods

    View Slide

  12. View Slide

  13. Use collections in place of arrays

    View Slide

  14. Use collections in place of arrays

    View Slide

  15. Use collections in place of arrays

    View Slide

  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.

    View Slide

  17. Code Examples
    https://github.com/tampabaylaravel/understanding-collections/tree/master/tests/Feature

    View Slide