Laravel 7 Create Custom Helper Function

Laravel 7

Nicesnippets

3947

06-03-2020


Laravel 7 Create Custom Helper Function

Hi Guys,

This article will provide some of the most important example Laravel 7 create custom helper. it's simple example of how to create custom helper in laravel 7. you will learn create helper function in laravel 7. you can understand a concept of add custom helpers in laravel 7.

I will show you how to get create custom hepler function in laravel 7. in your laravel 7 project some functions and some repeating code, so at that time you have to create some helper functions that can help to easy way and every time you not need to write more code and very flexible, that way you can easily modify that code.

Now you can add a custom helper function in your project. you can follow this step.

Step 1: Create helpers.php File

In this step, you need to create app/helpers.php in your laravel project and put the following code in that file.

app/helpers.php

<?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

In this step, you have to put path of helpers file,so basically open composer.json file and put following code in that file.

composer.json

"autoload": {

"classmap": [

...

],

"psr-4": {

"App\\": "app/"

},

"files": [

"app/helpers.php"

]

},

Step 3: Run Command

This is the last step, you should just run following command:

composer dump-autoload

Ok, now at last you can use your custom helper functions like changeDateFormate() and productImagePath(), i am going to give you example how to use custom helper functions.

Example 1

$imageName = 'example.png';

$fullpath = productImagePath($imageName);

print_r($fullpath);

Example 2

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

It will help you...


Recommended Posts