Laravel has a lot of helper functions that provide a convenient way of working with paths, arrays, and strings. It is almost impossible to know them all. Most developers know the most common ones, but there are some very useful helpers if you dig a little deeper. In this article, I will be giving you ten helper functions that you should know about.

1. Logger

The logger helper function can be used to write a message with a debug level to the log.

logger('Product was changed.', ['id' => $product->id]);

Which results in adding the following line to the log:

[2019-09-14 09:53:22] local.DEBUG: Product was changed. {"id":4}

If you don’t pass any value to the logger function, it will return a Logger instance. This allows you to write messages of different levels to the log.

logger()->error('An error occurred');

This adds the following line to the log:

[2019-09-14 09:56:12] local.ERROR: An error occurred

2. Dividing an array

The Arr::divide() method lets you split up an array in two arrays. The divide method returns two arrays. One array containing the keys and the other array containing the values.

use Illuminate\Support\Arr;[$keys, $values] = Arr::divide(['name' => 'James', 'age' => 33]);$keys: ['name', 'age']
$values: ['James', 33]

3. Blank

The blank helper function checks whether a value is “blank”. A “blank” value means null, a string only containing whitespaces or an empty array or string.

Note:
Booleans are 
not considered “blank” values.

blank('');
blank(' ');
blank(null);
blank(collect());// Will result in: trueblank(0);
blank(true);
blank(false);// Will result in: false

The inverse of this helper function is the filled helper function.

4. Dumping variables

Dumping variables is very handy if you want to debug one or more variables.

dump($variable);

It is also possible to dump multiple variables by passing extra variables to the dump function.

dump($var1, $var2, $var3);

Besides the dump helper function, there is another dump helper function. This helper function is called dd , which means “dump and die”. This function works the same as the dump function. Rather than just dumping the variable the dd function will also end the execution of the script.

5. Paths

Laravel has multiple helper functions that you can use to get the fully qualified path to certain directories.

These are the helper functions that Laravel has when it comes to paths:

  • app_path
  • base_path
  • config_path
  • database_path
  • public_path
  • resource_path
  • storage_path
echo storage_path();// Output:
"C:\Path\To\My\Project\storage"

You can also pass an argument to the path helper functions, which will get appended to the path:

echo storage_path('attachment.pdf');// Output:
"C:\Path\To\My\Project\storage\attachment.pdf"

6. Slug

To generate a URL friendly string from a given string you can use the Str::slug helper.

$slug = Str::slug('Laravel Is Awesome');$slug: "laravel-is-awesome"

The default separator is a hyphen (-), but you can overwrite this by passing a second argument to the function.

$slug = Str::slug('Laravel Is Awesome', '&');$slug: "laravel&is&awesome"

7. Array has value

The Arr:has method can be used to check whether an item or multiple items exist in an array using the “dot” notation.

To check for multiple items simply pass an array instead of a string to the method.

use Illuminate\Support\Arr;$blogs = ['blog' => ['title' => 'My blog', 'published' => true]];$contains = Arr::has($blogs, 'blog.title'); 
// true$contains = Arr::has($blogs, ['blog.title', 'blog.published']);
// true$contains = Arr::has($blogs, ['blog.title', 'blog.author']);
// false

8. UUID

The Str::uuid method generates a UUID:

use Illuminate\Support\Str;echo(string) Str::uuid(); // "2ad4abcc-8adc-47b6-b21e-9e5497a8af1b"

9. Optional

The optional helper function allows you to access properties or call methods on an object that you pass as an argument. Any argument is accepted by this function.

If the object that was passed to the function is null, properties and methods will return null instead of causing an error.

print optional($blog->author)->full_name;

If the blog author is set in the example above, then the full name of the author will be printed. If for some reason there is no author there won’t be an error and nothing will be printed.

10. Pluck

The Arr::pluck method retrieves all of the values for a given key from an array.

$parents = [
['parent' => ['id' => 1, 'name' => 'James']],
['parent' => ['id' => 8, 'name' => 'Lisa']],
];Arr::pluck($parents, 'parent.name'); // ['James', 'Lisa']

Reference from medium.com