laravel/helpers
Backwards-compatibility layer that restores Laravel 5.8 global helper functions in modern Laravel releases. Install via Composer and use legacy helpers while migrating to the equivalent Arr and Str methods.
Installation:
composer require laravel/helpers
No additional configuration is required—helpers are globally available after installation.
First Use Case:
Replace deprecated global helpers (e.g., array_get()) with their modern equivalents while maintaining backward compatibility:
// Legacy helper (still works)
$value = array_get($array, 'key.default');
// Modern equivalent (recommended for new code)
$value = Arr::get($array, 'key.default');
Where to Look First:
Arr and Str classes (Laravel Helpers Docs).v1.8.3 supports Laravel 12 and PHP 8.5).Legacy Code Migration:
// Old code (works with package)
$firstItem = head($collection);
// New code (modern alternative)
$firstItem = $collection->first();
Third-Party Package Compatibility:
laravel/helpers to restore functionality without forking:
// Example: Old package using `str_contains()`
if (str_contains($string, 'search')) { ... }
Testing:
public function test_array_get_returns_default()
{
$array = ['key' => null];
$this->assertEquals('default', array_get($array, 'key.default'));
}
phpunit with --coverage to track helper usage in legacy tests.IDE Integration:
// phpstorm.meta.php
{
"laravel/helpers": {
"functions": ["array_get", "str_contains", ...]
}
}
Upgrade Workflow:
laravel/helpers and upgrade Laravel core.Arr/Str.CI/CD Integration:
# .github/workflows/test.yml
- run: php artisan test -- --filter="DeprecationWarningTest"
roave/security-advisories to scan for Laravel version conflicts.Documentation:
UPGRADING.md:
[ ] Replace `array_get()` with `Arr::get()`
[ ] Replace `str_contains()` with `Str::contains()`
Arr::get() over array_get() in new code to reduce future migration effort.Arr/Str Directly: For new projects, skip the package entirely and use Laravel’s built-in classes:
// Preferred (no package needed)
use Illuminate\Support\Arr;
Arr::get($array, 'key');
Helper facade:
use Illuminate\Support\Facades\Helper;
Helper::array_get($array, 'key');
Infinite Loops:
<1.8.0) had bugs in array_first(), array_last(), and str_contains() that could cause infinite loops.>=1.8.0 and ensure PHP 8.1+ is used.Deprecated Function Warnings:
error_reporting(E_ALL & ~E_DEPRECATED);
PHP Version Mismatches:
<8.1 in newer releases (e.g., v1.8.2 requires PHP 8.5).1.7.2 for PHP 8.1) if needed.Array Prepend/Append Issues:
array_prepend() and array_append() may not pass all arguments correctly in older versions (<1.4.1).>=1.4.1 or manually call Arr::prepend():
Arr::prepend($array, ...$values);
Testing Edge Cases:
public function test_nested_array_get()
{
$array = ['user' => ['name' => 'John']];
$this->assertEquals('John', array_get($array, 'user.name'));
}
Helper Not Found:
laravel/helpers is listed in composer.json and autoloaded.composer dump-autoload and verify the vendor/autoload.php includes the package.Behavior Mismatches:
$this->assertEquals(
array_get(['a' => 1], 'a', 'default'),
Arr::get(['a' => 1], 'a', 'default')
);
Performance Overhead:
Arr/Str calls in hot paths.Helper facade, ensure it’s not overridden by other packages:
// Check for conflicts
composer show --tree | grep helper
trait CustomHelpers {
public function customHelper($arg) { ... }
}
macro system to extend Arr/Str:
Arr::macro('customMethod', function ($array) { ... });
app/Providers/AppServiceProvider:
public function boot()
{
if (!function_exists('custom_helper')) {
function custom_helper($arg) { ... }
}
}
Leverage Arr/Str Features:
Arr/Str classes offer more features than global helpers (e.g., Arr::dot(), Str::of()).// Global helper
str_contains($str, 'search');
// Modern alternative
Str::contains($str, 'search'); // + more methods like `Str::of()->contains()`
Automated Refactoring:
array_get( → Replace with Arr::get(.Document Migration Progress:
// TODO: Replace `array_get()` with `Arr::get()` in this method
$value = array_get($data, 'path');
Monitor Usage:
How can I help you explore Laravel packages today?