Laravel Helpers are utility functions that simplify common programming tasks in Laravel, a popular PHP web application framework. Laravel Helpers are pre-defined PHP functions that you can call in your code without requiring you to define them yourself.
Laravel offers a range of built-in Helper functions that provide various functionalities, such as manipulating arrays, generating URLs, encrypting data, and more. These Helper functions help you write cleaner and more concise code, improving the overall efficiency of your Laravel application.
Moreover, Laravel allows you to create your own custom Helper functions to extend the functionality of your application. Custom Helper functions can be defined in a separate PHP file, which can be loaded in your application using Composer.
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);
// Output
// ['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');
// Output
// ['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');
// Output
// 100
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']);
// Output
// ['name' => 'Desk', 'price' => 100]
Arr::prepend()
The Arr::prepend
method will push an item onto the beginning of an array:
use Illuminate\Support\Arr;
$array = ['one', 'two', 'three', 'four'];
$array = Arr::prepend($array, 'zero');
// Output
// ['zero', 'one', 'two', 'three', 'four']
If needed, you may also specify the key that should be used for the value:
use Illuminate\Support\Arr;
$array = ['price' => 100];
$array = Arr::prepend($array, 'Desk', 'name');
// Output
// ['name' => 'Desk', 'price' => 100]
Arr::sortRecursive()
The Arr::sortRecursive
method recursively sorts an array using the sort
function for numerically indexed sub-arrays and the ksort
function for associative sub-arrays:
use Illuminate\Support\Arr;
$array = [
['Roman', 'Taylor', 'Li'],
['PHP', 'Ruby', 'JavaScript'],
['one' => 1, 'two' => 2, 'three' => 3],
];
$sorted = Arr::sortRecursive($array);
// Output
/*
[
['JavaScript', 'PHP', 'Ruby'],
['one' => 1, 'three' => 3, 'two' => 2],
['Li', 'Roman', 'Taylor'],
]
*/
Arr::wrap()
The Arr::wrap
method wraps the given value in an array. If the given value is already an array it will be returned without modification:
use Illuminate\Support\Arr;
$string = 'Laravel';
$array = Arr::wrap($string);
// Output
// ['Laravel']
If the given value is null
, an empty array will be returned:
use Illuminate\Support\Arr;
$array = Arr::wrap(null);
// Output
// []
auth()
The auth
function returns an authenticator instance. You may use it as an alternative to the Auth
facade:
$user = auth()->user();
If needed, you may specify which guard instance you would like to access:
$user = auth('admin')->user();
dump()
The dump
function dumps the given variables:
dump($value);
dump($value1, $value2, $value3, ...);
If you want to stop executing the script after dumping the variables, use the dd
function instead.
optional()
The optional
function accepts any argument and allows you to access properties or call methods on that object. If the given object is null
, properties and methods will return null
instead of causing an error:
return optional($user->address)->street;
{!! old('name', optional($user)->name) !!}
The optional
function also accepts a closure as its second argument. The closure will be invoked if the value provided as the first argument is not null:
return optional(User::find($id), function (User $user) {
return $user->name;
});