How to Create Custom Helper Functions in Laravel

techiydude
3 min readJun 21, 2023

--

Custom Helper

Understanding Helpers in Laravel

Laravel provides helpers as an easy way to assist in small tasks without having to write complex code. These helpers are functions that can be called throughout the application without the need to import or instantiate them first. Some of the most commonly used helpers are those for strings, arrays, and paths. While Laravel provides a wide range of helpers out-of-the-box, developers can also create their own custom helpers to cater to their development needs.

What Justifies a Custom Helper?
Even while Laravel includes a number of pre-built helpers, there are situations when developers may have highly particular needs that the default helpers cannot satisfy. Custom aids are in beneficial in this situation. Developers can write custom helper methods to meet the specific requirements of their projects thanks to custom helpers. This can significantly improve development time by reducing the time needed to do repeated tasks.

How Can You Make a Custom Helper in Laravel?

  1. Inside the “app” folder and create a new file called Helpers.php.
  2. Define your helper functions: Inside the PHP file, define your custom helper functions using regular PHP syntax. These functions should encapsulate the functionality you want to provide. For example:
 if (!function_exists('custom_helper_function')) {
function custom_helper_function($parameter1, $parameter2)
{
// Your custom logic here
return $desired_output;
}
}

if (!function_exists('formatPrice')) {
function formatPrice($amount)
{
// Your custom logic here
return '$' . number_format($amount, 2); }
}

3.Laravel automatically load helpers.php , if file is present otherwise we need to add helper in composer.json file.

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

4.Make sure to add the files entry inside the autoload if it doesn’t exist already.

5.After changes in the composer.json file , we need to run the following command in your terminal to update the autoloader:

composer dump-autoload

Now,Your Helper is Ready to use,You can use your custom helper function in your application. You can call it from your controllers, views, or any other location where you need it.

<?php

use App\CustomHelper;

class MyController extends Controller
{
public function myMethod()
{
$result = formatPrice(200);
// $result will be $200.00

// Rest of your code
}
}

Remember to clear the cache of application (php artisan cache:clear) if you don’t see the changes taking effect immediately.

If you want to learn how laravel traite works in laravel , so you can read this tutorial:

And if you love the content and want to support more awesome articles, consider buying me a coffee! ☕️🥳 Your support means the world to me and helps keep the knowledge flowing. You can do that right here: 👉 [Buy Me a Coffee](https://buymeacoffee.com/jainaman)

“No one in the brief history of computing has ever written a piece of perfect software. It’s unlikely that you’ll be the first.” — Andy Hunt

--

--

techiydude
techiydude

Written by techiydude

I’m a developer who shares advanced Laravel and Node.js insights on Medium.

Responses (3)