laravel/helpers
Backwards-compatibility package that restores Laravel 5.8 global helper functions for newer Laravel versions. Useful when upgrading legacy apps; helpers map to modern Arr and Str methods. Not accepting new helpers.
Installation:
composer require laravel/helpers
No additional configuration is required—autoloading handles the facade aliases automatically.
First Use Case: Replace deprecated Laravel 5.8 helpers with their modern equivalents without code changes:
// Legacy (Laravel 5.8) → Works in Laravel 9+
array_add($array, 'key', 'value'); // → Arr::set($array, 'key', 'value');
str_limit('long string', 20); // → Str::limit('long string', 20);
Html::decode('&entity;'); // → html_entity_decode() or Str::of('...')->decode();
Where to Look First:
Facade-Based Replacement:
Use facades like Arr, Str, or Html to call legacy helpers transparently:
// Before (Laravel 5.8)
$value = array_get($array, 'key.default');
// After (Laravel 9+ with package)
$value = Arr::get($array, 'key', 'default'); // Works via facade alias
Migration Workflow:
Arr::set() instead of array_add()).Integration with Legacy Code:
// config/app.php
'aliases' => [
'Arr' => Illuminate\Support\Facades\Arr::class,
'Str' => Illuminate\Support\Facades\Str::class,
],
Arr::shouldReceive('get')->andReturn(...) in PHPUnit to mock legacy calls during migration.Performance Considerations:
Arr/Str classes.array_add() + Arr::set()) to prevent confusion.Upgrade Path:
Laravel 5.8 → [Install helpers package] → Laravel 9+ → [Refactor to modern helpers] → Remove package
Plugin/Third-Party Compatibility:
helpers package.CI/CD Integration:
// phpstan.neon
rules:
Laravel\Helpers\DeprecatedHelperRule: true
Infinite Loop Risks:
array_first(), array_last(), or str_contains() may cause infinite loops in edge cases (fixed in v1.8.0+). Test with:
$result = Arr::first([], fn($item) => true); // Returns null (safe)
Null Handling:
array_get()) return null for missing keys, while modern Arr::get() uses a default value. Test edge cases:
Arr::get($emptyArray, 'nonexistent', 'default'); // Returns 'default'
Facade Alias Conflicts:
Arr/Str facades, the package may not work. Ensure no custom aliases exist in config/app.php.PHP 8.4+ Strict Typing:
array_prepend()) may throw type errors. Use func_get_args() workarounds or update calls to match modern signatures.No New Helpers:
Missing Helper Errors:
composer show laravel/helpers).Arr/Str classes are autoloaded (run composer dump-autoload).Method Not Found:
array_except → Arr::except()).Performance Issues:
Modern Equivalents Cheat Sheet:
| Legacy Helper | Modern Equivalent |
|---|---|
array_add() |
Arr::set() |
array_get() |
Arr::get() |
str_limit() |
Str::limit() |
Html::decode() |
html_entity_decode() |
Arr::dot() |
Arr::dot() (already modern) |
Refactoring Strategy:
# Replace array_add with Arr::set (exclude vendor/)
grep -r "array_add(" --exclude-dir=vendor | xargs sed -i 's/array_add(/Arr::set(/g'
Testing Legacy Helpers:
Arr facade in tests to verify behavior:
$this->mock(Arr::class)->shouldReceive('get')->andReturn('mocked');
Laravel Version Quirks:
array_first) may behave differently due to PHP 8.5+ changes. Test thoroughly.Arr::where() instead of array_where() (deprecated in L9).Extension Points:
Str::macro('customLimit', ...)).ArrHelper trait to modify default logic:
use Laravel\Helpers\ArrHelper;
class CustomArr extends ArrHelper { ... }
How can I help you explore Laravel packages today?