Skip to main content

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 like data_get()
  • Numbers — the Number:: class
  • Paths — path retrieval functions like app_path() and storage_path()
  • URLs — URL generation functions like route(), url(), and asset()
  • Miscellaneous — commonly used utilities like config(), collect(), and auth()
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.
The data_get() global function has the same capability. It also handles Arrayable objects (such as Eloquent relationships), so it’s usable in more general situations.

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

collect() is the most important helper as the entry point to collections. See Collections for details.

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

Best practice is to not call env() directly from controllers or service classes, but rather to call it inside config/ files and access the values via config(). When configuration caching (php artisan config:cache) is enabled, env() may not return the value you expect.

Choosing between the Arr class and collect()

The Arr:: 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

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
When to use 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, or groupBy
Last modified on July 13, 2026