How to Create Custom Helper Functions in Laravel 8 Tutorial

Share this Article

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.


Installation of Laravel 8 Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel 8 already installed at 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.

Find More on Laravel 8 Articles here

Leave a Comment

What are CSRF Functions in CodeIgniter 4 Tutorial
MySQL Stored Procedure in CodeIgniter 4 Tutorial