Write Less Code with Laravel Higher Order Messages

Eric McWinNEr
Backend Developers
Published in
2 min readNov 24, 2022
Photo by Kelly Sikkema on Unsplash

In this article, I’ll show you how you can make your code shorter and cleaner when working with Laravel Collections. I’ll be focusing on this cool thing I recently discovered called Higher Order Messages. This article assumes you have working knowledge of Laravel and Laravel Collections.

What are Higher Order Messages?

Higher Order Messages have been around since Laravel 7, but I only recently discovered them. Higher Order messages are short-cuts for performing functions on collections. It’s mostly used to modify actions that loop through items of a Laravel collection.

These are the collection methods that support Higher Order Messages: average, avg, contains, each, every, filter, first, flatMap, groupBy, keyBy, map, max, min, partition, reject, skipUntil, skipWhile, some, sortBy, sortByDesc, sum, takeUntil, takeWhile, and unique.

So here’s a code snippet taken directly from the documentation of how it works:

use App\Models\User;

$users = User::where('votes', '>', 500)->get();

$users->each->markAsVip();

Looks pretty cool right? Well, what’s actually going on there? Let’s give a quick example of the kind of code I was writing that helped me discover this concept.

A Simple Use Case — Iteration

Imagine we’re writing some code to pick out all the users of a database and filter them based on some criteria defined in our model. Let’s say this is our User model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes;

public function filterSomeUsers() {
// Filter logic we don't care about right now
}
}

If I wanted to filter users with the filterSomeUsers method, I’d probably write something like this a couple of weeks ago:

$users = User::all();
$filteredUsers = $users->filter(function($user) {
return $user->filterSomeUsers();
})->values();

We can make this code shorter using PHP 8 arrow functions:

$users = User::all();
$filteredUsers = $users->filter(fn($user) => $user->filterSomeUsers())
->values();

However using the Higher Order Messages, we can get even shorter code:

$users = User::all();
$filteredUsers = $users->filter->filterSomeUsers()->values();

Laravel is smart enough to know to run the filterSomeUsers method on each item in the collection when the filter loops through the collection.

Another Simple Usecase — Modification

In the section above, we saw how we can specify an action to perform on all items that a collection method is running on. Take a collection method like sum , you might want to specify the key we’re trying to add up or a method like groupBy key we’re trying to group the collection by. We can do the same via Higher Order Messages.

$users = User::all();
$averageScore = $users->sum->exam_scores / $user->count();

Conclusion

We learned about Higher Order Messages and how they can make your code a lot cleaner and fluent to read. Thank you for reading, I’ll catch you in the next one.

--

--

Eric McWinNEr
Backend Developers

Full-Stack Developer, anime enthusiast, philomath, human. Follow me on Twitter @ericmcwinner