Laravel 6 Create Custom Helper Function

  • 29-03-2022
  • 1451
  • Laravel 6
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In this post, I will give you easy steps on how to create a custom helper function file in Laravel 6. I will give some step by step instructions of what is the procedure to create a custom helper function in the Laravel 6 application. As we know Laravel 6 also provides Helper function for SEO META, URL, ARRAY, Path and you can create common function.

You can create a one-time helper function and multiple times You can use the same code. So it's helper function is the best way to use everywhere same code. Now let's Create Laravel 6 Custom Helper function By Using the following steps. If you want to use Laravel 6 Custom Helper function in your project then you have to follow just 3 steps and use it as per your requirements.

Step 1 : Here, we require to create Helpers.php File

In this step we need to create app/Helpers.php file and put below code.

app/helpers.php

<?php
function status($val){
    if($val == 1):
        echo $active = 'Active';
    else:
        echo $deactive = 'Deactive';
    endif;
}

function gender($data) {
    if($data == 0):
        return 'Male';
    else:
        return 'Female';
    endif;
}

function chackeAuth(){
    return \Auth::guard('frontuser')->check()?TRUE:FALSE;
}

function getCurrencyData($currency_code) {
    $currency_symbols = array(
        'EUR'=>['name' => 'Euro', 'symbol'=>'€', 'hex'=>'€'],
        'USD'=>['name' => 'US Dollar', 'symbol'=>'$', 'hex'=>'$'],
        'INR'=>['name' => 'Indian Rupee', 'symbol'=>'₹', 'hex'=>'₹'],
    );
    $data = $currency_symbols[$currency_code];
    return $output = (object) $data;
}

Step 2 : Put Helper.php File Path In composer.json File

Here, We require to set Helper.php file path on composer.json So, Now open composer.json put bellow code into file.

composer.json

"autoload": {

    "classmap": [
        ...
    ],

    "psr-4": {
        "App\\": "app/"
    },

    "files": [
        "app/Helpers.php"
    ]
},

Step 3 : Run Command

In this step we require to run below command.

composer dump-autoload

Laravel 6 Custome Helper function Ready for run So,Now you can access Helper file function anywhere.

Example

{{ status(1) }}

Output
// Active


{{ getCurrencyData('INR')->name }}

Output
// Indian Rupee

I hope this Laravel 6 Custom helper function example it can be helpful for you.

Thanks...


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :