How to Create Custom Helper Functions in Laravel 11

How to Create Custom Helper Functions in Laravel 11

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.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to How to Create Custom Helper Functions in Laravel 11

  1. Thanks for clean tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *