10 Laravel Helpers That You Should Know

10 Laravel Helpers That You Should KnowDaanBlockedUnblockFollowFollowingJun 15Laravel has a lot of helper functions which 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.

LoggerThe 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-06-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-06-14 09:56:12] local.

ERROR: An error occurred2.

Dividing an arrayThe 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 IlluminateSupportArr;[$keys, $values] = Arr::divide(['name' => 'James', 'age' => 33]);$keys: ['name', 'age']$values: ['James', 33]3.

BlankThe 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: falseThe inverse of this helper function is the filled helper function.

4.

Dumping variablesDumping 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.

PathsLaravel 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_pathbase_pathconfig_pathdatabase_pathpublic_pathresource_pathstorage_pathecho storage_path();// Output:"C:PathToMyProjectstorage"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:PathToMyProjectstorageattachment.

pdf"6.

SlugTo 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 valueThe 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 IlluminateSupportArr;$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']); // false8.

UUIDThe Str::uuid method generates a UUID:use IlluminateSupportStr;echo(string) Str::uuid(); // "2ad4abcc-8adc-47b6-b21e-9e5497a8af1b"9.

OptionalThe 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.

PluckThe 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']These are the ten Laravel helpers that I wanted to share with you.

Please share your thoughts on this article.

Make sure to check out my other posts as well.

A lot of my content is about Laravel.

Feel free to leave a comment if you have any feedback, questions or want me to write about another Laravel related topic.

.

. More details

Leave a Reply