Laravel comes with a ton of useful global helper functions. If you haven’t used them so far, this is the best time to start. Over the years of me using the Laravel, 10 of those emerged as the most useful, making the development a lot easier. Sometimes we don’t really see how we can apply some methods until we see an example. So let’s get down to it and see the top 10 helpers I often use the most.
These go for Laravel 5.*, however those on Laravel 6.* can bring these back using the following package https://github.com/laravel/helpers.
You can also check out the official documentation for all laravel helper functions.
The array_flatten function flattens a multi-dimensional array into a single level array:
$flattened = array_flatten($array);
// ['John', 'Laravel', 'Recipes']
It is really helpful if I need to collect all the array values where some values might contain another array. Here, I am just concentrated on getting a new array with a list of all the values. It works like a charm!
The array_forget function removes a given key / value pair from a deeply nested array using “dot” notation:
// ['users' => ]
This is a nicer version of unset() function which is a native PHP function for removing array elements.
Another amazing method that makes your development life easier. The array_get function retrieves a value from a deeply nested array using “dot” notation:
$price = array_get($array, 'products.desk.price');
The array_get function also accepts a default value, which will be returned if the specific key is not found:
If there is anything more valuable than getting a deeply nested value withing an array, that’s an ability to set a default value.
Imagine you had a lot of keys inside your array that you don’t want to use. As a matter of fact, out of 10 keys, you only want to use two and instantly create a new array. Instead of going through and array_forget()each item, you could simply pick the ones you want. The array_only function returns only the specified key / value pairs from the given array:
$slice = array_only($array, ['name', 'age']);
// ['name' => 'John', 'age' => 44]
How often have you used array_push and had to reverse the array instead of pre-pending it. The array_prepend function will push an item onto the beginning of an array:
$array = array_prepend($array, 'zero');
// ['zero', 'one', 'two', 'three', 'four']
It’s great that it works for key/value as well. If needed, you may specify the key that should be used for the value:
$array = array_prepend($array, 'Desk', 'name');
// ['name' => 'Desk', 'price' => 100]
Many times you would get nested arrays that you might need to sort all at the same time. Yes, you could write a simple function to loop through and sort each array, but why, when you have the following function. The array_sort_recursive function recursively sorts an array using the sort function:
['Roman', 'Taylor', 'Li'],
$sorted = array_sort_recursive($array);
['Li', 'Roman', 'Taylor'],
Sometimes you want to turn your single, string result into an array with only one element. Being able to reduce code to one line is always good. The array_wrap function wraps the given value in an array. If the given value is already an array it will not be changed:
You want to have your public files, such as the application icons, svg image, css resources etc… that are used statically inside the app, in your public folder. The public_path function will bring back the fully qualified path to the public directory. You may also use the public_path function to generate a fully qualified path to a given file within the public directory:
$path = public_path('css/app.css');
Probably used the most, auth() doesn’t require you to insert the Auth facade. It works simple and easy on the fly and I use it mostly to get the currently logged in user. The auth function returns an authenticator instance. You may use it instead of the Auth facade for convenience:
If needed, you may specify which guard instance you would like to access:
If you want to change your realm and do all of this with collections, and I love collections, like really love them, then you can bridge the array and collections using the collect() function. The collect function creates a collection instance from the given value:
The dump() function dumps the given variables without stopping the execution. It is extremely useful for debugging since it pretty-prints the whole class for you, in case you would print an Eloquent Model object.
dump($var1, $var2, $var3);
If you do not want to continue executing your script, use the dump function we mentioned above. However, if you are interested in inspecting a specific result and don’t care about what happens after that, then use dd(). The dd function dumps the given variables and ends execution of the script:
dd($value1, $value2, $value3, ...);
You have probably run into this issue at least once in your dev lifetime, and that is trying to access a property that doesn’t exist. The optional() function accepts an argument and you can call its methods or access properties. If the passed object is null, methods and properties will return null instead of causing errors or throwing exceptions.
That would be it. These are the Laravel helpers function I find extremely helpful. They help me reduce the amount of code I need to write and make it at least a bit more bulletproof.