To create a custom helper functions in Laravel 11; Simply create helpers.php
in app/ directory, define your functions in that, then add custom helper file path in composer.json and run composer update and use custom helper function.
In laravel custom helper, you can create your 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 reduce the re-writing of the same code repeatedly. 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 11 Create and Use Custom Helper Function Example
Steps to create and use helpers file in laravel. Use the below steps for that:
1. Create helpers.php File
Simply navigate to the app directory and create helpers.php
. And then add your codes in helpers.php; like following:
<?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(
' ' => '-', '!' => '', '"' => '',
'#' => '', '$' => '', '%' => '',
'&' => '', '\'' => '', '(' => '',
')' => '', '*' => '', '+' => '',
',' => '', '₹' => '', '.' => '',
'/-' => '', ':' => '', ';' => '',
'<' => '', '=' => '', '>' => '',
'?' => '', '@' => '', '[' => '',
'\\' => '', ']' => '', '^' => '',
'_' => '', '`' => '', '{' => '',
'|' => '', '}' => '', '~' => '',
'-----' => '-', '----' => '-', '---' => '-',
'/' => '', '--' => '-', '/_' => '-',
);
foreach ($specChars as $k => $v) {
$t = str_replace($k, $v, $t);
}
return $t;
}
2. Add Helper File Path In composer.json File
To add helper file path in composer.json file, simply navigate to project root directory and open composer.json file, and then add helpers.php path like following:
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php"
]
},
3. Run Composer Auto Load Command
Just open your cmd or terminal window and type the following command into it and press enter:
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 removes the special character from the given string. If you want to modify this function you can do:
$str = "remove & 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.
Thanks for clean tutorial.