Hello Devs,

In this tutorial, we are going to learn how to get create custom hepler function in laravel 7.

Follow this step by step guide given below:




Step 1:

 

Create helpers.php File

<?php
  
    function changeDateFormate($date,$date_format){
        return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($date_format);    
    }
   
    function productImagePath($image_name)
    {
        return public_path('images/products/'.$image_name);
    }



Step 2: 


Add File Path In composer.json File

"autoload": {
        "classmap": [
            ...
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/helpers.php"
        ]
    },



Step 3: 


Run Command

composer dump-autoload


Example 1

$imageName = 'example.png';
   $fullpath = productImagePath($imageName);
   print_r($fullpath);


Example 2

{{ changeDateFormate(date('Y-m-d'),'m/d/Y')  }}


I hope this example helps you.