Helpers
Introduction
Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.
Available Methods
Arrays & Objects
Arr::accessible Arr::add Arr::collapse Arr::crossJoin Arr::divide Arr::dot Arr::except Arr::exists Arr::first Arr::flatten Arr::forget Arr::get Arr::has Arr::hasAny Arr::isAssoc Arr::last Arr::only Arr::pluck Arr::prepend Arr::pull Arr::query Arr::random Arr::set Arr::shuffle Arr::sort Arr::sortRecursive Arr::toCssClasses Arr::undot Arr::where Arr::whereNotNull Arr::wrap data_fill data_get data_set head last
Paths
app_path base_path config_path database_path mix public_path resource_path storage_path
Strings
__ class_basename e preg_replace_array Str::after Str::afterLast Str::ascii Str::before Str::beforeLast Str::between Str::camel Str::contains Str::containsAll Str::endsWith Str::finish Str::headline Str::is Str::isAscii Str::isUuid Str::kebab Str::length Str::limit Str::lower Str::markdown Str::mask Str::orderedUuid Str::padBoth Str::padLeft Str::padRight Str::plural Str::pluralStudly Str::random Str::remove Str::replace Str::replaceArray Str::replaceFirst Str::replaceLast Str::reverse Str::singular Str::slug Str::snake Str::start Str::startsWith Str::studly Str::substr Str::substrCount Str::substrReplace Str::title Str::toHtmlString Str::ucfirst Str::upper Str::uuid Str::wordCount Str::words trans trans_choice
Fluent Strings
after afterLast append ascii basename before beforeLast between camel contains containsAll dirname endsWith exactly explode finish is isAscii isEmpty isNotEmpty isUuid kebab length limit lower ltrim markdown mask match matchAll padBoth padLeft padRight pipe plural prepend remove replace replaceArray replaceFirst replaceLast replaceMatches rtrim scan singular slug snake split start startsWith studly substr substrReplace tap test title trim ucfirst upper when whenContains whenContainsAll whenEmpty whenNotEmpty whenStartsWith whenEndsWith whenExactly whenIs whenIsAscii whenIsUuid whenTest wordCount words
URLs
action asset route secure_asset secure_url url
Miscellaneous
abort abort_if abort_unless app auth back bcrypt blank broadcast cache class_uses_recursive collect config cookie csrf_field csrf_token dd dispatch dump env event filled info logger method_field now old optional policy redirect report request rescue resolve response retry session tap throw_if throw_unless today trait_uses_recursive transform validator value view with
Method Listing
Arrays & Objects
Arr::accessible()
The Arr::accessible
method determines if the given value is array accessible:
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
// true
$isAccessible = Arr::accessible(new Collection);
// true
$isAccessible = Arr::accessible('abc');
// false
$isAccessible = Arr::accessible(new stdClass);
// false
Arr::add()
The Arr::add
method adds a given key / value pair to an array if the given key doesn't already exist in the array or is set to null
:
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
Arr::collapse()
The Arr::collapse
method collapses an array of arrays into a single array:
use Illuminate\Support\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
Arr::crossJoin()
The Arr::crossJoin
method cross joins the given arrays, returning a Cartesian product with all possible permutations:
use Illuminate\Support\Arr;
$matrix = Arr::crossJoin([1, 2], ['a', 'b']);
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);
/*
[
[1, 'a', 'I'],
[1, 'a', 'II'],
[1, 'b', 'I'],
[1, 'b', 'II'],
[2, 'a', 'I'],
[2, 'a', 'II'],
[2, 'b', 'I'],
[2, 'b', 'II'],
]
*/
Arr::divide()
The Arr::divide
method returns two arrays: one containing the keys and the other containing the values of the given array:
use Illuminate\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']
Arr::dot()
The Arr::dot
method flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = Arr::dot($array);
// ['products.desk.price' => 100]
Arr::except()
The Arr::except
method removes the given key / value pairs from an array:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
// ['name' => 'Desk']
Arr::exists()
The Arr::exists
method checks that the given key exists in the provided array:
use Illuminate\Support\Arr;
$array = ['name' => 'John Doe', 'age' => 17];
$exists = Arr::exists($array, 'name');
// true
$exists = Arr::exists($array, 'salary');
// false
Arr::first()
The Arr::first
method returns the first element of an array passing a given truth test:
use Illuminate\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
// 200
A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:
use Illuminate\Support\Arr;
$first = Arr::first($array, $callback, $default);
Arr::flatten()
The Arr::flatten
method flattens a multi-dimensional array into a single level array:
use Illuminate\Support\Arr;
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$flattened = Arr::flatten($array);
// ['Joe', 'PHP', 'Ruby']
Arr::forget()
The Arr::forget
method removes a given key / value pair from a deeply nested array using "dot" notation:
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::forget($array, 'products.desk');
// ['products' => []]
Arr::get()
The Arr::get
method retrieves a value from a deeply nested array using "dot" notation:
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');
// 100
The Arr::get
method also accepts a default value, which will be returned if the specified key is not present in the array:
use Illuminate\Support\Arr;
$discount = Arr::get($array, 'products.desk.discount', 0);
// 0
Arr::has()
The Arr::has
method checks whether a given item or items exists in an array using "dot" notation:
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::has($array, 'product.name');
// true
$contains = Arr::has($array, ['product.price', 'product.discount']);
// false
Arr::hasAny()
The Arr::hasAny
method checks whether any item in a given set exists in an array using "dot" notation:
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::hasAny($array, 'product.name');
// true
$contains = Arr::hasAny($array, ['product.name', 'product.discount']);
// true
$contains = Arr::hasAny($array, ['category', 'product.discount']);
// false
Arr::isAssoc()
The Arr::isAssoc
returns true
if the given array is an associative array. An array is considered "associative" if it doesn't have sequential numerical keys beginning with zero:
use Illuminate\Support\Arr;
$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
// true
$isAssoc = Arr::isAssoc([1, 2, 3]);
// false
Arr::last()
The Arr::last
method returns the last element of an array passing a given truth test:
use Illuminate\Support\Arr;
$array = [100, 200, 300, 110];
$last = Arr::last($array, function ($value, $key) {
return $value >= 150;
});
// 300
A default value may be passed as the third argument to the method. This value will be returned if no value passes the truth test:
use Illuminate\Support\Arr;
$last = Arr::last($array, $callback, $default);
Arr::only()
The Arr::only
method returns only the specified key / value pairs from the given array:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$slice = Arr::only($array, ['name', 'price']);
// ['name' => 'Desk', 'price' => 100]