In this tutorial we will see how to Create Custom Helper Functions in Laravel 8. Before moving further we need to understand first that Laravel framework already come up with ready-made helper function, So why we need to create our own custom helper functions.
So the reason is sometimes we were having custom requirement in the project like we need date format in specific format only ( Jan-2021 ), I that case we can not use the existing helper function.

Now we will start with tutorial, How to Create Custom Helper Functions in Laravel 8.
Let?s start with step by step procedure.

Table of Content 

  1. Create helpers.php File
  2. Add Helper File Path In composer.json File
  3. Regenerate All Classes in Application
  4. How to Use Helper Function

1. Create helpers.php File

In this step we will see how to create helper file.
In Laravel we don?t have any restriction to create a custom helper file into specific folder location, we can create anywhere in the project just we need to add that specific folder location into compose.json file.
For now we will create custom helper file in app/helper.php folder. Please create the file into given location, Open the helper.php file and paste the below code.

<?php
 
if(!function_exists("replaceWhiteSpaceWithHyphen")){
 
    function replaceWhiteSpaceWithHyphen($string){
 
        return strtolower(str_replace(" ", "-", $string));
    }
}

Here, We have created the simple function in which we are replaced blank space with Hypen. Now we will see how to use this function in our code, but before that we have to add file path into composer.json file.

2. Add File Path In composer.json File

In the step 2 we will add the hepler.php file path location into composer.json file. Please check below code.

...
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "app/helpers.php"
    ]
},
...

3. Regenerate All Classes in Application

In tis step we need to run the below command to Regenerate All Classes in Application.

composer dump-autoload

4. How to Use Helper Function

In the last step we will check How to Use Helper Function. We can use this helper function multiple ways. Let?s check one by one

<div>
 @php
	$string = replaceWhiteSpaceWithHyphen("Code Solution Stuff");
 @endphp
	{{ $string }}
</div>
...
$string = replaceWhiteSpaceWithHyphen("Code Solution Stuff");
...
<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SampleController;
 
Route::get('/', function () {
   
    $string = replaceWhiteSpaceWithHyphen("Code Solution Stuff");
 
    echo $string;
});

I hope you will like the content and it will help you to learn Laravel 8 Create Custom Helper Functions
If you like this content, do share.


Recommended Posts

View All

Laravel 9 Form Validation Example


In this Laravel 9 validation tutorial, I'll demonstrate how to validate form input and provide an error message before saving it to the database.

Laravel One to Many Polymorphic Relationship


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

Laravel 9 Image Resize & Upload with Intervention Image


This tutorial will show you how to use the PHP intervention image package to resize an image in a Laravel application.

Laravel One to One Eloquent Relationship Tutorial


Laravel One To One Eloquent Relationships Tutorial Example, Laravel Eloquent ORM is the best possible database solution, ORM Mapping is very easy, lar...

Laravel Many to Many Polymorphic Relationship


Laravel many to many polymorphic relations, many to many polymorphic relations Laravel, polymorphic relations eloquent Laravel, Laravel morphToMany ex...