How to Create Custom Helper Functions in Laravel 8 Tutorial

Share this Article
Reading Time: 3 minutes
9,178 Views

Helper functions are standalone functions which can be used through out application inside components, controllers, view files etc. By default Laravel also provides the pre defined helper functions as view(), base_path(), url(), asset() etc.

In some cases we need to create custom helper functions.

Inside this article we will discuss about How to create custom helper functions in laravel 8. This will be very interesting topic. There are very few simple steps which we need to follow to create a helper function.

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Step #1 – Create Helper File

In laravel 8, there is no specific folder location defined in application structure to store custom helper files. So we can create anywhere either at root or inside /app folder.

Let’s create a file helpers.php inside /app folder.

Open /app/helpers.php file and write this code into it.

<?php

if(!function_exists("removeWhiteSpace")){

    function removeWhiteSpace($string){

        return strtolower(str_replace(" ", "-", $string));
    }
}

Here, we have defined a simple basic function which replaces white space from hypen symbol and turns string into lowercase value.


Step #2 – Add into composer.json File

We need to include file helpers.php file into composer.json (you will find this file at application root), So that when application runs, it will autoload the file.

...

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

...

Step #3 – Regenerate All Classes in Application

Open project into terminal and run this command.

$ composer dump-autoload

Usage of Helper Function

Now, we will see how to use custom helper function in application.

Using into View

<div>
     @php
        $string = removeWhiteSpace("Online Web Tutor");
     @endphp

        {{ $string }}
 </div>
OUTPUT

online-web-tutor

Using into Controller

//...

$string = removeWhiteSpace("Online Web Tutor");

//...

Using into Closure Routes

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SampleController;

Route::get('/', function () {
  
    $string = removeWhiteSpace("Online Web Tutor");

    echo $string;
});
OUTPUT

online-web-tutor

We hope this article helped you to learn about How to Create Custom Helper Functions in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5