Published on September 07 Aug 25
Laravel Hidden Gems: 25+ Helper Functions You Should Know (with Before vs. After Examples)

Laravel Hidden Gems: 25+ Helper Functions You Should Know (with Before vs. After Examples)
Laravel includes dozens of helper functions that simplify everyday tasks. In this post, weβll explore 25+ underrated Laravel helpers and show how they clean up your code with clear before vs. after examples.
π 1. append_config()
Use case: Safely append items to config arrays (used in package configs).
π΄ Before:
return [
'providers' => [
'My\Package\Provider',
0 => 'App\CustomProvider',
]
];
π’ After:
return [
'providers' => append_config([
'My\Package\Provider',
0 => 'App\CustomProvider',
]),
];
π 2. blank()
Checks if a value is empty (but smarter than empty()
).
π΄ Before:
if (is_null($value) || trim($value) === '') {
// ...
}
π’ After:
if (blank($value)) {
// ...
}
π 3. filled()
The opposite of blank()
.
π΄ Before:
if (! is_null($value) && trim($value) !== '') {
// ...
}
π’ After:
if (filled($value)) {
// ...
}
π 4. class_basename()
Gets the class name without the namespace.
π΄ Before:
$class = get_class($user);
$parts = explode('\\', $class);
$name = end($parts);
π’ After:
$name = class_basename($user);
π 5. class_uses_recursive()
Get all traits used by a class and its parents.
π΄ Before:
$traits = array_merge(
class_uses(MyClass::class),
class_uses(get_parent_class(MyClass::class))
);
π’ After:
$traits = class_uses_recursive(MyClass::class);
π 6. e()
Escape HTML output.
π΄ Before:
echo htmlspecialchars($input, ENT_QUOTES);
π’ After:
echo e($input);
π 7. env()
Get environment variable.
π΄ Before:
$env = $_ENV['APP_ENV'] ?? 'production';
π’ After:
$env = env('APP_ENV', 'production');
π 8. fluent()
Make data array fluent (dot-access).
π΄ Before:
$data = ['name' => 'Yassin'];
$name = $data['name'];
π’ After:
$data = fluent(['name' => 'Yassin']);
$name = $data->name;
π 9. literal()
Quickly create an object with named arguments.
π΄ Before:
$obj = (object) ['name' => 'Laravel', 'version' => 11];
π’ After:
$obj = literal(name: 'Laravel', version: 11);
π 10. object_get()
Get deeply nested object property using dot notation.
π΄ Before:
$email = $user->profile->email ?? null;
π’ After:
$email = object_get($user, 'profile.email');
π 11. laravel_cloud()
Detect if running in Laravel Cloud.
π΄ Before:
$isCloud = ($_ENV['LARAVEL_CLOUD'] ?? false) === '1';
π’ After:
$isCloud = laravel_cloud();
π 12. once()
Run a closure only once per request.
π΄ Before:
static $value;
if (! isset($value)) {
$value = computeSomething();
}
π’ After:
$value = once(fn () => computeSomething());
π 13. optional()
Prevent null errors when chaining.
π΄ Before:
$name = $user ? $user->name : null;
π’ After:
$name = optional($user)->name;
π 14. preg_replace_array()
Sequential regex replacement.
π΄ Before:
$str = 'Hello @, meet @.';
$str = str_replace_first('@', 'John', $str);
$str = str_replace_first('@', 'Jane', $str);
π’ After:
preg_replace_array('/@/', ['John', 'Jane'], 'Hello @, meet @.');
π 15. retry()
Retry a block of code with delay.
π΄ Before:
try {
// try
} catch (Exception $e) {
sleep(1);
// try again
}
π’ After:
$result = retry(3, fn () => doSomething(), 100);
π 16. str()
Get a fluent stringable object.
π΄ Before:
Str::of('Laravel')->slug();
π’ After:
str('Laravel')->slug();
π 17. tap()
Perform a side-effect and return the value.
π΄ Before:
$user->update([...]);
Log::info($user);
return $user;
π’ After:
return tap($user)->update([...]);
π 18. throw_if()
Throw if condition is true.
π΄ Before:
if (! $user) {
throw new Exception('User not found');
}
π’ After:
throw_if(! $user, 'User not found');
π 19. throw_unless()
Throw if condition is false.
π΄ Before:
if (! $user->isAdmin()) {
throw new UnauthorizedException();
}
π’ After:
throw_unless($user->isAdmin(), UnauthorizedException::class);
π 20. trait_uses_recursive()
List all used traits recursively.
π΄ Before:
$traits = class_uses(MyTrait::class);
π’ After:
$traits = trait_uses_recursive(MyTrait::class);
π 21. transform()
Only transform a value if it's present.
π΄ Before:
$name = $value ? strtoupper($value) : 'N/A';
π’ After:
$name = transform($value, fn ($v) => strtoupper($v), 'N/A');
π 22. windows_os()
Check if current OS is Windows.
π΄ Before:
if (PHP_OS_FAMILY === 'Windows') { ... }
π’ After:
if (windows_os()) { ... }
π 23. with()
Apply optional callback to value.
π΄ Before:
if ($callback) {
return $callback($value);
}
return $value;
π’ After:
return with($value, $callback);
π§ Summary
These Laravel helper functions donβt just reduce code β they add clarity, readability, and safety to your application. Next time you're writing repetitive isset()
, is_null()
, or try/catch
blocks β ask yourself:
"Is there a Laravel helper for this?"
Youβll be surprised how often the answer is yes.