What are helper functions
Laravel provides a large number of global PHP functions (helpers). While the framework itself uses most of them internally, you’re free to use them in your application as well. Helpers fall into these main categories:- Arrays and objects — the
Arr::class and functions likedata_get() - Numbers — the
Number::class - Paths — path retrieval functions like
app_path()andstorage_path() - URLs — URL generation functions like
route(),url(), andasset() - Miscellaneous — commonly used utilities like
config(),collect(), andauth()
Helper functions can be called from anywhere without a
use declaration. To use the Arr or Number class, you need imports like use Illuminate\Support\Arr;.Array helpers (Arr class)
Arr::get() — retrieve nested values using dot notation
Safely retrieve nested values from a multi-dimensional array using dot notation (foo.bar.baz). Returns a default value if the key does not exist.
Arr::set() — set a value in a nested array
Arr::has() — check if a key exists
Arr::only() / Arr::except() — filter or exclude keys
Handy when you want to extract only the necessary keys from request data or configuration arrays, or exclude unneeded keys.
Arr::pluck() — extract a specific key from a nested array
Arr::first() / Arr::last() — the first/last element that matches a condition
Arr::flatten() — flatten a multi-dimensional array
Arr::wrap() — ensure a value is an array
If the value is not an array, wrap it in one. null becomes an empty array. Useful when accepting flexible arguments to functions.
Arr::sort() — sort by value
Arr::dot() / Arr::undot() — convert to and from dot notation
Arr::join() — join an array into a string
data_get() — access nested data
The general-purpose version of Arr::get(). It supports not just arrays but also objects, Eloquent models, and collections.
Number helpers (Number class)
Number::format() — format numbers for readability
Number::currency() — currency formatting
Number::fileSize() — human-readable file sizes
Number::abbreviate() — abbreviate large numbers
Number::percentage() — display as a percentage
Path helpers
Retrieve directory paths within your application. They return the correct path regardless of environment or deployment target.URL helpers
route() — generate a URL for a named route
url() — absolute URL for an arbitrary path
asset() — URL for a static asset
to_route() — redirect to a named route
Other commonly used helpers
config() — get and set configuration values
collect() — create a collection
auth() — get the authenticated user
blank() / filled() — check for emptiness
blank() treats null, empty string, whitespace-only, empty collections, and empty arrays as true. filled() is the inverse.
abort() / abort_if() / abort_unless() — HTTP exceptions
dd() / dump() — debugging
dispatch() — push a job onto the queue
encrypt() / decrypt() — encryption
env() — get an environment variable
Choosing between the Arr class and collect()
TheArr:: class operates on regular PHP arrays; collect() returns collection objects.
Eloquent results are automatically returned as collections, so there’s no need to wrap them with
collect(). When simple array operations are enough, using Arr:: keeps things simpler.Summary
Common helpers reference
Common helpers reference
Arr class vs collect()
Arr class vs collect()
When to use the
Arr:: class:- Simple array operations (accessing nested keys, filtering keys, etc.)
- When the collection object’s overhead is unnecessary
- When you want to keep the result as an array
collect():- When you want to express multiple operations via method chains
- When further transforming Eloquent results (already collections)
- When you want to use collection-specific methods like
map,filter, orgroupBy