Laravel is a very important framework while creating an API. API use tokens when they authenticate their users and they create no gap session between processing requests. Laravel is a PHP based framework that is used to create web applications and follows MVC architecture pattern. It also helps you in re-usable components to perform daily tasks with ease and comfort. Many web services require token-based authentication from API to protect their website security. Laravel passport provides an OAuth2 server for your Laravel web development in a matter of minutes. It generates its database migration and the command will setup encrypted keys to generate secure access to tokens.
In this article, we will discuss the 10 best Laravel helpers that you should know. These helper functions are used to run the Laravel framework without any hurdle.
1. Dump ()
Dump () functions dump the variables without disturbing the process. It is very useful in debugging.
Use: dumb($var1);dumb($var1,$var2,$var3);
2. Logger
The logger function is mostly used to write a message with a debug level.
Use: logger (‘Product was changed.’, [‘id’ => $product->id]);
3. Blank
The blank function work on null values. It checks whitespaces and blank array in a string.
Use: blank (”); blank(‘ ‘); blank(null); blank(collect());//
Will result in: true blank(0); blank(true); blank(false);// Will result in: false
4. Optional ()
This function works on an error that you face at least once in life. When you search for non-existing property or value, the optional function encrypts the information. If data doesn’t exist it will return in null value rather than showing an error.
5. Paths
Laravel has many functions that help in getting qualified paths. These paths include:
6. Slug
Slug is used for generating a friendly URL. Hyphen – is defaulter separator. You can use this Str: slug this helper.
7. Collect ()
If you want to change your principle by creating collections, you can cross over the array and collection by using the collection () function.
8. Dividing the array
This function includes the division of two arrays. One array consists of keys and the other array consists of values.
use Illuminate\Support\Arr;
9. Public path ()
The public files include SVG images, app icons, CSS resources are in inside the app and you want them. The public_path function fully retrive those files in public directory.It is also used to generate a qualified path.
Use this helper:
$path = public_path(); $path = public_path(‘css/app.css’);
10. Array only ()
If you have large number of keys but you don’t want to use them all. For example, if you have 20 keys but you only use 2 keys so except running array forget () function and going through every item. Use the array only() method and picks the exact keys you want to use.
Use this:
$array = [‘name’ => ‘John’, ‘type’ => ‘user’, ‘age’ => 44]; $slice = array only ($array, [‘name’, ‘age’]); // [‘name’ => ‘John’, ‘age’ => 44]