In this post we will see the what is helper, how to use and most used helper function example in Laravel.
Laravel provide variety of global helper PHP function, in which Laravel framework itself used most of the function. So by our own requirement we can use or implement this global helper function in your application. You can also create your own helper which I covered in another blog you can checkout below.

In this post we will 10 Helper Function Example in Laravel 8. Following is the list of 10 Helper Function.

Helper Function Example in Laravel 8 
1. csrf_token()
2. csrf_field()
3. cookie()
4. bcrypt()
5. dd()
6. now()
7. Str::between
8. Str::camel
9. Str::contains
10. Arr::random

Let?s see one by one helper function with example.
1. csrf_token() : CSRF means Cross-site request forgeries, Server side application generates unique, secret and inconsistent value. Laravel easily protect our application from cross-site request forgery (CSRF) attacks.

$token = csrf_token();

2. csrf_field() : csrf_field() is used to generate hidden field in HTML Form which contains CSRF Token Value. We have to write this function in curly braces inside the form tag.

{{ csrf_field() }}

3. cookie() : We can create cookie by using global helper function cookie(). When we talk about user sessions on web application cookie plays major role. cookie() functions takes three parameters.

Name

Value

Duration ( You can also set it to lifetime. )

$cookie = cookie('name', 'value', $minutes);

4. bcrypt() : bcrypt() is a password-hashing function. You can use bcrypt() to convert your password into bcrypt.

$pwd = bcrypt('password');

5. dd() : dd() helper means display and die. It will dump the given variable and stop the execution of script.

dd($users);

6. now() : Returns current time

$time = now();

7. Str::between : Before using this function we need to add use Illuminate\Support\Str; this line. To find small portion of the between two string use Str::between.

use Illuminate\Support\Str;
$slice = Str::between('This is Code Solution Stuff Example', 'is', 'example');
// it will return ' Code Solution Stuff '

8. Str::camel : Convert assigned string into camel case.

use Illuminate\Support\Str;
$example = Str::camel('Code_Solution');
// CodeSolution

9. Str::contains : This method is case sensitive. This method returns TRUE when given string contains the given value in the method otherwise returns FALSE.

use Illuminate\Support\Str;
$contains = Str::contains('This is Code Solution Stuff Example', 'demo');
// false

10. Arr::random : Before using this function we need to add use Illuminate\Support\Arr; this line. When you have array contains the value and need to find out the random number out of it use this method

use Illuminate\Support\Arr;
$array = [10, 20, 30, 40, 50];
$random = Arr::random($array);

I hope you will like the content and it will help you to learn Helper Function Example in Laravel 8
If you like this content, do share.


Recommended Posts

View All

Laravel One to Many Polymorphic Relationship


laravel polymorphic relations example, laravel polymorphic one to many, laravel eloquent polymorphic relations, laravel one to many polymorphic

How to Implement Gravatar Image using thomaswelton/laravel-gravatar in Laravel 9


Learn how to easily implement Gravatar image using thomaswelton/laravel-gravatar in Laravel 9. Enhance user profiles with personalized images.

Laravel 9 Livewire Form Submit Tutorial


Laravel 9 Work With Livewire Form Submit Tutorial. laravel 9 livewire form submit example, create form with laravel 9 livewire, laravel 9 livewire tut...

Effortlessly Handle File Uploads in Laravel with FilePond


We'll walk through the process of setting up FilePond, creating a file upload form, and handling file uploads in Laravel

Create Zip file and Download in Laravel 9


After we have compressed the files, let's download the Laravel 9 zip files. In this tutorial, we'll use the ZipArchive class and a Laravel package to...