DEV Community

Cover image for From Rejected Laravel PR to Laravel Arr Extended: Adding Arr::after
Gulfaraz Arshad
Gulfaraz Arshad

Posted on

From Rejected Laravel PR to Laravel Arr Extended: Adding Arr::after

Have you ever been working with a Laravel array and thought, "I really wish there was an Arr::after() method right now"?

I did — so I submitted a PR to Laravel core.

The idea was simple: a helper that returns the element immediately after a given value in an array. The PR didn't make it in, which is completely understandable. Taylor mentioned it felt too niche for the framework core, and Laravel intentionally keeps things lean. Not every utility belongs there.

Instead of dropping the idea, I turned it into a package: Laravel Arr Extended.

The Problem

Before this, getting the next element in an array looked like this:

$items = ['a', 'b', 'c'];
$current = 'a';
$index = array_search($current, $items);
$next = isset($items[$index + 1]) ? $items[$index + 1] : null;
Enter fullscreen mode Exit fullscreen mode

That's three lines of boilerplate you have to rewrite every time — and it doesn't even handle associative arrays or wrap-around. It gets messier fast.

Introducing Arr::after()

With Laravel Arr Extended, you can now do this:

use GulfarazArshad\LaravelArrExtended\Arr;

Arr::after(['a', 'b', 'c'], 'a'); // 'b'
Enter fullscreen mode Exit fullscreen mode

It also supports wrap-around behavior, so the last element loops back to the first:

Arr::after(['a', 'b', 'c'], 'c', wrap: true); // 'a'
Enter fullscreen mode Exit fullscreen mode

And it works with associative arrays too:

Arr::after(['x' => 'a', 'y' => 'b'], 'a'); // 'b'
Enter fullscreen mode Exit fullscreen mode

If the value isn't found in the array, it returns null — no exceptions, no surprises:

// Value not found — returns null by default
Arr::after(['a', 'b', 'c'], 'z'); // null

// Value not found with wrap — returns first element
Arr::after(['a', 'b', 'c'], 'z', wrap: true); // 'a'
Enter fullscreen mode Exit fullscreen mode

Type Safety

The method uses strict comparison under the hood — no type juggling:

Arr::after([1, 2, 3], '1'); // null (string !== integer)
Arr::after([1, 2, 3], 1);   // 2 ✅
Enter fullscreen mode Exit fullscreen mode

Why I Added It

I kept finding myself rewriting the same combination of array_search(), manual index calculations, and repetitive edge-case handling across projects. Arr::after() makes that logic reusable, readable, and consistent.

Real World Examples

Step wizard — null signals completion:

$steps = ['personal', 'address', 'payment', 'confirm'];

$next = Arr::after($steps, 'payment'); // 'confirm'
$done = Arr::after($steps, 'confirm'); // null — wizard complete!
Enter fullscreen mode Exit fullscreen mode

Carousel with wrap-around:

$slides = ['home', 'about', 'contact'];

$next = Arr::after($slides, 'contact', wrap: true); // 'home'
Enter fullscreen mode Exit fullscreen mode

Round-robin load balancing:

$servers = ['server1', 'server2', 'server3'];

$next = Arr::after($servers, 'server3', wrap: true); // 'server1'
Enter fullscreen mode Exit fullscreen mode

Day of week rotation:

$days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];

$next = Arr::after($days, 'Sun', wrap: true); // 'Mon'
Enter fullscreen mode Exit fullscreen mode

Installation

composer require gulfaraz-arshad/laravel-arr-extended
Enter fullscreen mode Exit fullscreen mode

Check It Out

If you find it useful, a ⭐ on GitHub goes a long way. And if you've run into other array utilities you keep rewriting, I'd love to hear them — this package is growing.


Have you run into similar gaps in Laravel's array helpers? Drop a comment — I'd love to know what utilities you keep rewriting.

Top comments (0)