DEV Community

Cover image for 10 Essential Laravel Helpers You Should Be Using
Aleson França
Aleson França

Posted on

10 Essential Laravel Helpers You Should Be Using

Laravel offers many global helper functions that make your code cleaner and easier to write. In this post, I’ll show you 10 helper functions I often use in real projects — they can help you write better and more readable code.


class_basename()

Get only the class name, without the namespace.

class_basename(App\Models\User::class); // 'User'
Enter fullscreen mode Exit fullscreen mode

collect()

Create a collection from an array great for working with data.

collect([1, 2, 3])->map(fn($n) => $n * 2); // [2, 4, 6]
Enter fullscreen mode Exit fullscreen mode

str() and Str::of()

Useful for working with strings in a fluent way.

Str::of('Laravel')->lower()->append(' rocks!'); // 'laravel rocks!'
Enter fullscreen mode Exit fullscreen mode

throw_if() and throw_unless()

Throw an exception if a condition is true or false.

throw_if(!$user, new Exception('User not found'));
Enter fullscreen mode Exit fullscreen mode

value()

Returns a value or runs a closure and returns the result.

$value = value(fn() => 'Generated dynamically');
// 'Generated dynamically'
Enter fullscreen mode Exit fullscreen mode

retry()

Try something again a few times until it works or fails.

$response = retry(3, function () {
    return Http::get('https://api.example.com');
}, 100);
Enter fullscreen mode Exit fullscreen mode

tap()

Run an action on a value and return the value.

$user = tap(User::find(1), function ($user) {
    $user->logAccess();
});
Enter fullscreen mode Exit fullscreen mode

blank() and filled()

Check if a value is "empty" or "filled", more flexible than empty().

blank(''); // true
filled('Laravel'); // true
Enter fullscreen mode Exit fullscreen mode

optional()

Avoids errors when working with null objects.

$user = null;
$name = optional($user)->name; // null, no error
Enter fullscreen mode Exit fullscreen mode

data_get()

Safely get a value from a nested array or object.

$data = ['user' => ['name' => 'Ana']];
$name = data_get($data, 'user.name'); // 'Ana'
Enter fullscreen mode Exit fullscreen mode

Conclusion

Laravel helpers are great tools for writing clean, expressive code. If you’re not using them yet, give them a try in your next project and feel the difference.

Top comments (1)

Collapse
 
xwero profile image
david duymelinck

I'm not a big fan of the Laravel helpers.

Some hide dependencies.

function collect($value = [])
 {
   return new Collection($value);
}
Enter fullscreen mode Exit fullscreen mode

Some are obsolete in most cases, like the optional function. The PHP nullsafe operator handles most of the cases.

Some are just for the laziest of the lazy developer, like throw_if. Just add an if statement.

For me the tap function is just dangerous. It has too much power.

Before you use the helpers I suggest to evaluate if you need them. You can have clean and expressive code without the helpers.