Laravel — Create Custom Helper With Example

Sagarvermaitdeveloper
1 min readMay 19, 2020

Hello Today We will make a custom Helper and learn how it will Work

All Right create Fresh Laravel Project with this command

composer create-project --prefer-dist laravel/laravel blog "6.*"

then we need to make a controller so we make a controller with this command

php artisan make:controller DemoController -r

Please always use camel case and 1st letter be capital so with this we follow the standard pf code

Make a Helper directory in app folder. example = app/Helper

Make a php file helper.php in helper directory

paste this code in app/Helper/helper.php

function random_code(){

return rand(1111, 9999);
}

Now we need to add the file in composer.json, Just paste this code in composer.json

"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": ["app/Helper/helper.php"]
},

And run this command in your terminal

composer dump autoload

Example 1

Paste this code into your composer and check the value

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

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, Tech Sagar

--

--