Tap, Tap, Tap

Tap might be the simplest helper of Laravel yet if used more often can help you write a cleaner and readable code. Quite often I find myself writing long strings of code just to modify a variable in some form. Honestly, it always ends the same way, the dirty way.

What tap() do’s is, it takes a variable as it’s the first argument and then passes it through the callback given as 2nd argument. A typical example of tap, as used by Laravel is:

<?php

public function create(array $attributes = [])
{
return tap($this->newModelInstance($attributes), function ($instance) {
$instance->save();
});
}

Just recently, Tylor has improved this tiny little helper to shorten tap() calls even more. He call’s it HigherOrderTapProxy. It lets you pass an object to tap and then tap its methods to return the object itself, for e.g. we can use tap to update a model record and return the object itself instead of a boolean value.

<?php

return tap($user)->update([
'name' => $name,
'age' => $age,
]);

Tylor wrote a post on this tiny little helper and said:

While tap is a very simple helper, I find it often lets me write terse, one-line operations that would normally require temporary variables or additional lines.

PS: I know, I copied title from original Taylor’s post, but after reading it, I just couldn’t help it. It stuck in my mind. :P

 Tap,  Helpers,  Higher order,