Laravel 8 Create Custom Helper Function Example

Websolutionstuff | Oct-12-2020 | Categories : Laravel

In this article, we will see laravel 8 create a custom helper function example. As we all know laravel provides many readymade functions in their framework, but many times we need to require our own customized function to use in our project that time we need to create a custom helper function. So, here we will learn a custom helper function example in laravel 8.

So, let's see the custom helper function in laravel 8.

How To Create Custom Helper Functions In Laravel 8?

Step 1: Create helpers.php File

In this step, we will create an app/helpers.php file in our laravel project and add the below code.

app/helpers.php

<?php
  
function change_Date_Format($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": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
         "files": [
            "app/helpers.php"
        ]
    },

 

Step 3: Run Command

Now, run the below command in your terminal

composer dump-autoload

 So, we are done with the custom helper function in laravel 8, as of now we can use this function anywhere.

Here I am using this function in the blade file.

<html>
 <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title>Laravel 8 Create Custom Helper Functions Example - websolutionstuff.com</title>
     <link rel="stylesheet" href="">
 </head>
 <body>
    <h3>Laravel 8 Create Custom Helper Functions Example - websolutionstuff.com</h3>
     <h3>New Date Format: {{ change_Date_Format(date('Y-m-d'),'m/d/Y')  }}</h3>
 </body>
</html>

 

 

Output:

laravel_8_create_helper_functions_example

 


You might also like:

Recommended Post
Featured Post
How To Convert PHP Array To JSON Object
How To Convert PHP Array To JS...

In this article, we will explore the process of converting a PHP array into a JSON object. We'll achieve this transf...

Read More

Jul-08-2020

Carbon Add Minutes To Date In Laravel 9
Carbon Add Minutes To Date In...

In this article, we'll explore how to add minutes to a date in Laravel 8, Laravel 9 and Laravel 10 using Carbon...

Read More

Nov-23-2022

How To Upload Large CSV File Using Queue In Laravel 9
How To Upload Large CSV File U...

In this article, we will see how to upload a large CSV file using queue in laravel 9. Here we will learn large numb...

Read More

Sep-16-2022

Laravel Datatables Example
Laravel Datatables Example

In this example, I will show you how to implement/install data tables in laravel. Datatables provides users with many fu...

Read More

May-16-2020