Laravel 9 Create Custom Helper Function in Tutorial

Share this Article
Reading Time: 4 minutes
312 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 Laravel 9 Create Custom helper functions. This will be very interesting topic. There are very few simple steps which we need to follow to create a helper function.

Learn More –

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 9, 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 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). 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

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 Laravel 9 Create Custom Helper Functions Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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