How To Create Custom Helper Function in Laravel 10 Tutorial

Reading Time: 4 minutes
552 Views

Creating custom helper functions in Laravel 10 is an excellent method to encapsulate common functionality and expedite your development process. You may encapsulate repetitive activities, increase code readability, and promote code reusability throughout your Laravel application by creating your own helper functions.

In this detailed article, we will walk you through the process of building custom helper methods in Laravel 10. We will look at various strategies and best practises for defining and implementing custom helpers in your application.

Read More: How To Create Custom Log File in Laravel 10 Tutorial

A helper function in Laravel is a pre-defined function that simplifies common programming chores and improves code readability. These functions are available everywhere in the application and can be invoked from anywhere without the need for instantiation or import.

By default Laravel also provides the pre defined helper functions as view(), base_path(), url(), asset() etc.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

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 10, 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 helpers.php from /app folder 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.

Read More: How To Create Custom Route File in Laravel 10 Tutorial

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). Once you add this line into autoload, custom helper file will be autoload once application executes.

...

"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

This will dump your application’s composer.json and loads all autoload classes once.

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

Read More: How To Create Custom Artisan Command in Laravel 10

Using into Controller

//...

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

//...

Using into Closure Routes

<?php

use Illuminate\Support\Facades\Route;

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 Function in Laravel 10 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more