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 full-size slide

  2. What are collections?

    View full-size 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 full-size 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 full-size 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 full-size slide

  6. Why use them?

    View full-size slide

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

    View full-size slide

  8. Method Chaining

    View full-size slide

  9. Method Chaining

    View full-size slide

  10. Method Chaining

    View full-size slide

  11. Method Chaining: Helper Methods

    View full-size slide

  12. Use collections in place of arrays

    View full-size slide

  13. Use collections in place of arrays

    View full-size slide

  14. Use collections in place of arrays

    View full-size slide

  15. 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 full-size slide

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

    View full-size slide