Global Helpers Introduction
Laravel defines many global helper functions you can call anywhere without importing.
Laravel registers dozens of global helper functions that are always available — no use statement needed. They are shortcuts to common tasks and services.
Frequently used helpers
collect()— make a Collection.str()— make a fluent string.now()— the current date/time.value()— resolve a value or closure.tap()— run a callback then return the object.
Example
Loading editor…
Press Run to execute the code.
When to use it
- A developer uses the collect() helper inside a controller method to wrap a plain array and immediately chain groupBy() and map() without importing any class.
- A Blade template calls the now() helper directly in the view to render the current timestamp without passing it from the controller.
- An Artisan command uses the tap() helper to log a model before returning it, avoiding an extra variable assignment.
More examples
Most common global helpers at a glance
Shows four of the most frequently used global helpers: collect(), config(), now(), and the translation helper __().
// Wrap an array as a Collection
$items = collect([1, 2, 3]);
// Read a config value with a default
$locale = config('app.locale', 'en');
// Get the current Carbon instance
$now = now();
// Translate a string
$label = __('messages.welcome');Use env() and config() correctly
env() belongs only in config files; application code should always call config() so values work correctly after config:cache is run.
// .env
// DB_HOST=127.0.0.1
// config/database.php
'host' => env('DB_HOST', '127.0.0.1'),
// Everywhere else in application code
$host = config('database.connections.mysql.host');
// NEVER call env() outside config filesRoute and URL helpers
Demonstrates route(), asset(), and redirect(), the three URL-generation helpers used most in controllers and Blade templates.
// Generate a URL for a named route
$url = route('products.show', ['product' => 42]);
// => https://example.com/products/42
// Generate a URL to a public asset
$logo = asset('images/logo.png');
// => https://example.com/images/logo.png
// Redirect inside a controller
return redirect()->route('dashboard');
Discussion