Laravel – Create Custom Helper With Example
Laravel 18-Nov-2020

Laravel – Create Custom Helper With Example

Laravel create a custom helper. Here we will teach you how to create a custom helper in laravel application and how to call these functions anywhere in laravel project. We will explain to you each thing for creating and calling a custom helper function in laravel application.

In laravel custom helper, you can create your own function and call anywhere like route, blade view, models, controller etc in laravel project. It is best practice to code reusable and saves a lot of time to replicate the code.

A custom helper helps to reduce the re-writing the same code again and again. In this custom helper tutorial, we will show you an example of how you can create a function in your custom helper and how to call this function.

Laravel Custom Helper

Create helpers file in laravel 6. Use the below steps for that:

1. Create helpers.php File

In this step, we need to create helper.php in the laravel project inside the app directory. Update the below-given code into your file.

In this file, you can write our own functions and call anywhere in your laravel based project.

Go to App directory and create a new file new helpers.php like this app/helpers.php.

<?php
  
  function imploadValue($types){
    $strTypes = implode(",", $types);
    return $strTypes;
  }
 
  function explodeValue($types){
    $strTypes = explode(",", $types);
    return $strTypes;
  }
 
  function random_code(){
 
    return rand(1111, 9999);
  }
 
  function remove_special_char($text) {
 
        $t = $text;
 
        $specChars = array(
            ' ' => '-',    '!' => '',    '"' => '',
            '#' => '',    '$' => '',    '%' => '',
            '&amp;' => '',    '\'' => '',   '(' => '',
            ')' => '',    '*' => '',    '+' => '',
            ',' => '',    '?' => '',    '.' => '',
            '/-' => '',    ':' => '',    ';' => '',
            '<' => '',    '=' => '',    '>' => '',
            '?' => '',    '@' => '',    '[' => '',
            '\' => '',   ']' => '',    '^' => '',
            '_' => '',    '`' => '',    '{' => '',
            '|' => '',    '}' => '',    '~' => '',
            '-----' => '-',    '----' => '-',    '---' => '-',
            '/' => '',    '--' => '-',   '/_' => '-',   
             
        );
 
        foreach ($specChars as $k => $v) {
            $t = str_replace($k, $v, $t);
        }
 
        return $t;
  }

2. Add File Path In composer.json File

In this second step, you will add the path of the helpers file in the composer.json file. Let’s go to project root directory and open composer.json file and update the below-given code into the file:

composer.json

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

Run Command for autoloading

In this final step, go to command prompt and type the given command:

composer dump-autoload

After you have run the above command in your command prompt.

Now you can use your custom helper functions by calling this functions remove_special_char(), random_code() etc.

Here we will give you an example, how you can call your helper functions:

Example 1

The random_code() function is used to generate a new digits numeric code. If you want to modify this function you can do.

$code = random_code();
print_r($code);

Example 2

The remove_special_char function is used to remove the special character from the given string. You want to modify this function you can do.

$str = "remove &amp; special #character *in string@$ example."
$remove = remove_special_char($str);

print_r($remove);

//output
remove special character in string example

Conclusion

In this custom helper function tutorial, you have learned how to create custom helper and function. Also, you have learned how to call the custom helper function in a laravel based project.