Laravel Collections Higher Order Messages - Single Line Magic

ppshobi

Shobi

Posted on August 17, 2018

Laravel Collections Higher Order Messages - Single Line Magic

Collections are wrappers around arrays in PHP. Collections provide fluent interfaces and intuitive APIs to access arrays. Laravel comes with collections out of the box, and Laravel's eloquent ORM returns the result set in the form of collections by default.

Collections come with many useful methods which allow us to easily manipulate arrays, and they are chainable as well. Compared to the built-in PHP array manipulation functions, collections offers much readable and consistent syntax.

Here I will show you a little refactoring technique you can do while tinkering with collections using higher order messages.

Collections provide support for "higher order messages", which are short-cuts for performing common actions on collections. Documentation

Consider the scenario where you need to map over a collection and call method on the item

<?php
User.php
--------- 

/** 
 * the method returns the full name of the user 
 * by joining first and last name 
**/

function fullName(){
   return $this->first_name . ' ' . $this->last_name;
}

SomeFile.php
------------
/** 
 * In some other file, we need full names of all users 
**/

$fullNames = User::all()->map(function ($user) {
    return $user->fullName()
});

the above code shows how we do it normally with map(), but functions with the simple body as in the above code, we can use higher-order functions to cut the explicit crap.

<?php
$fullNames = User::all()->map->fullName();

Boom ⚡ Single line Magic 🔥❤️

Let's goto a little more complex scenario where we need to filter over a collection and then map over them again

<?php

User.php
---------
/** 
 * the method returns the full name of the user by joining first and last name 
**/

public function fullName() {
   return $this->first_name . ' ' . $this->last_name;
}

/** 
 * the method returns the if the first_name starts with a given letter 
**/

public function isStartingWith($letter) {
    return starts_with($this->first_name, $letter)
}

OtherFile.php
-------------
/** 
 * In some file, we need full names of all users which starts with 'A'
**/

$fullNamesWithA = User::all()->filter(function($user){
    return $user->isStartingWith('A');
})->map(function ($user) {
    return $user->fullName()
});

Let's try to use Collection Higher-order functions here and cut explicit syntax crap.

<?php
$fullNamesWithA = User::all()->filter->isStartingWith('A')->map->fullName();

Boom ⚡ Single line Magic 🔥❤️ Again

Adiós

💖 💪 🙅 🚩
ppshobi
Shobi

Posted on August 17, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related