How to Create Helper Functions in Laravel 10

How to Create Helper Functions in Laravel 10

Today we would be learning how to create a helper function in laravel 10, this is the same method in creating a helper function in other laravel version. 

what are Helper functions?

Helper function helps us to create a function that can be called anywhere in our app. That is a global function that can be called both in the views and also in the controller. In Laravel, a helper class is a utility class that provides a set of functions or methods that can be used throughout your application. These helper functions typically perform common tasks, such as formatting data, generating URLs, manipulating strings, and more. Laravel provides a variety of built-in helper functions, such as route, asset, url, and view, but you can also create your own custom helper classes to encapsulate reusable functionality.

Laravel and PHP also provide some basic functions that can be called anywhere, however, there are some times we might need to write our own custom functions that we will need both in the controller and in the views or other parts of our application.

To create a custom helper class in Laravel, you can follow these steps:

  1. Create a new folder and a PHP file in your application's app/Helpers/DateHelper.php directory (you may need to create this directory if it doesn't exist).
  2. Define a new class in this file, and give it a descriptive name that reflects the functionality it provides. For example, if you want to create a helper class for working with dates, you could name it DateHelper.
  3. Define one or more static methods in this class, each of which performs a specific task. For example, you could define a formatDate method that accepts a DateTime object and returns a formatted string.
  4. (Optional) Register your helper class with Laravel's auto-loading mechanism, so that it can be loaded automatically by the framework. To do this, open the composer.json file in your project's root directory, and add an entry to the autoload section that points to your helper class file. For example:
"autoload": {
    "files": [

Define your helper function in this file using the function keyword. For example, if you want to create a helper function for formatting a date, you could define it like this:

function formatDate(DateTime $date, string $format = 'Y-m-d'): string
    return $date->format($format);
//you can also use a static function like so
public static function formatDate(DateTime $date, string $format = 'Y-m-d'): string
    return $date->format($format);

Finally, you can use your custom helper function anywhere in your application, without the need to import or instantiate any classes. For example:

$date = new DateTime('2023-04-27');
$formattedDate = formatDate($date, 'Y-m-d'); // '2023-04-27'
//or if you are using a static function
$formattedDate = DateHelper::formatDate($date, 'Y-m-d'); // '2023-04-27'

You can also use the helper function to return results and response

public static function getEmployeeStatus($id=0){
        $record = Employees::find($id);

        return $record->status;

Make sure to run composer dump-autoload after specifying the file path in composer otherwise, you wouldn’t be able to access Helper functions.

Enjoy this article ? drop a comment below and add your own contribution.

Maye Jacob

Jacob Iyanuoluwa Olorunmaye

Verified Expert
I am a software engineer focused on full-stack web development with extensive experience with PHP, Laravel, Javascript, and Vue JS

Leave a Reply

Your email address will not be published. Required fields are marked *