Published on September 07 Aug 25

Laravel Hidden Gems: 25+ Helper Functions You Should Know (with Before vs. After Examples)

1 month ago 119

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.

#JavaScript #jQuery #laravel #php #elasticsearch #mongodb #mysql #wordpress #html #css #bootstrap #php8 #ui-development #tailwindcss #postgresql #redis #docker #openai