How to Create Custom Helper Functions In Laravel

What are helper functions in Laravel?

A Helper function is a small piece of code that performs a specific task or operation and can be used anywhere in your application. A helper function usually comes in handy if you want to add a feature to your Laravel application that can be used at multiple locations within your controllers, views, or model files. These functions can be considered as global functions.

This tutorial will guide you through creating a custom helper function in Laravel 10. While Laravel offers a wide range of built-in helper functions, there may be instances when you need to develop your own customized function for your project. In such cases, creating a custom helper function in Laravel 10 can be helpful.

Laravel provides various global helper PHP functions such as arrays, paths, strings, URLs, and more, which can be utilized based on your needs. Creating a custom helper function in Laravel 10 can help you extend the functionality of your project and improve its performance.

How to Create Custom Helper Functions In Laravel:

To create a helper function in Laravel, you need to create a new PHP file and specify the file path in the composer.json file. To create a custom helper function in Laravel 10, follow the steps below:

Step 1: Create helpers.php File inside 'app' folder in your Laravel project
<?php
  
function changeDateFormat($date,$format) 
{
    return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($format);    
}

?>
Step 2: Add Helper File Path In composer.json File

Add the below code in the composer.json file.

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php"
    ]
},
Step 3: Run Command

Once you add a new path to the files array in composer.json, you need to dump the autoloader:

composer dump-autoload

How to use Helper function in Laravel?

Now, we can use our custom Laravel helper function changeDateFormate() in our code.

<html>
 <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title>How To Create Custom Helper Function In Laravel 9 - Techsolutionstuff</title>
     <link rel="stylesheet" href="">
 </head>
 <body>
    <h3>How To Create Custom Helper Functions In Laravel 10 - Laravel Tutorials</h3>
     <h3>New Date Format: {{ Date_Format('2023-04-22','m/d/Y')  }}</h3>
 </body>
</html>

Output : 

New Date Format: 04/22/2023

Laravel Helper Function Examples:

Laravel has many useful helper functions. Please check the link below for a list of useful Laravel helper functions to use in your projects.


Search terms for this page:

  • how to create helper in laravel 8
  • create helper in laravel 7
  • laravel helper class
  • helper functions classes in laravel
  • create own custom helper functions

Related Questions:

  • How to create custom helper function in Laravel?
  • How to make custom helper function in Laravel?
  • What are helper functions in Laravel?

Leave a Comment