How To Create Custom Helper Function in Laravel 10 Tutorial

Share this Article
Reading Time: 4 minutes
189 Views

Inside this article we will see the concept of How To Create Custom Helper Function in Laravel 10 Tutorial. Article contains classified information about How to write custom helper function in Laravel Application.

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.

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

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.

Buy Me a Coffee

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.