Secret power of Laravel Collection

Secret power of Laravel Collection

Refactoring to collection.

This is article is reveiw of course.

Laravel Collection.

The Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data.

For a laravel developer, collection is a daily stuff. As the efficiency drives the speed, each topic of framework should be handy to who intends for faster development . This course really satisfies such requirement.

The instructor Adam Wathan is a well-known person in Laravel community.

What I have learned?

Test and guide to real world refactoring.

Adam never describes the rules, methods at first, rather he introduces a scenario from a real world project. That works fine. Then he starts refactoring. Here, you find the real test of refactoring.

Efficiency. This course helped me a lot to understand the power of laravel collection. Thus, it boosted my efficiency.

Tactics. 'After reading this book, every loop is now an opportunity to write cleaner, more expressive code.' Rainik said, I think so..

josh-boot-2x19-mRQgX8-unsplash.jpg

Code Snipet

//PHP 4, PHP 5, PHP 7, PHP 8
    $i = 'cake';
    switch ($i) {
        case 'cake':
            die('cake');
        case 'apple':
           die('apple');
        case 'banana':
           die('banana');
    }
//PHP 8
    $i = 'banana';
        $return_value = match ($i) {
            'cake' => 'Cake',
            'apple' => 'Apple',
            'banana' => 'Banana',
        };
    return $return_value;
//Laravel collection
    $collection = collect(['cake' => 'Cake', 'apple' => 'Apple', 'banana' => 'Banana',]);
    return $collection->get('apple');
    // Apple

From Book it self.

The Golden Rule of Collection Programming

Never use a foreach loop outside of a collection!Every time you use a foreach loop, you're doing it to accomplish something else, and I

promise you that "something else" already has a name. Need to loop over an array to perform some operation on each item and stuff the result

into another array? You don't need to loop, you need to map. Need to loop over an array to strip out the items that don't match some criteria? You

don't need to loop, you need to filter. Pipeline programming is about operating at a higher level of abstraction. Instead of

doing things with the items in a collection, you do things to the collection itself.

Map it, filter it, reduce it, sum it, zip it, reverse it, transpose it, flatten it, group it, count it, chunk it, sort it, slice it, search it; if you can do it with a foreach loop, you can do it with a collection method.

From this point forward, you won't see a single foreach anywhere other than encapsulated inside a collection method. Let the games begin

As soon as you elevate arrays from primitive types to objects that can have their own behavior, there's no reason to ever use a foreach loop outside of the collection itself, and I'm going to prove it to you.

Testimonials:

“I wrote the library used in this book, and I still learned dozens of tricks that have improved my code. This is the definitive resource for programming with collections.”

Taylor Otwell

“Adam's use of real-world examples for the more confusing collection transformations makes them approachable for anyone. I cannot recommend Refactoring to Collections enough.”

Matthew Machuga

Senior Developer at Think Through Math