Share this:

Hello dev, Today we are going to learn How to create custom helper functions in Laravel. Laravel, one of the most popular PHP frameworks, provides developers with a wealth of built-in functionalities and features.

However, there may be situations where you need to perform common tasks or add custom functionalities across your Laravel application. This is where custom helper functions come into play.

In this blog post, we’ll explore how you can create and utilize custom helper functions to streamline your Laravel development process.

Also Read: How to Seed US States in Database using Laravel 10 Seeder

Step 1: Create the Helper File

To begin, create a new file to store your custom helper functions. Laravel conventionally suggests placing these files in the app/Helpers directory, but you can choose any location that suits your project structure.

For consistency, we’ll follow the convention and create a new file called helpers.php in the app/Helpers directory.

Step 2: Define the Helper Functions

Open the helpers.php file and define your custom helper functions using PHP. These functions can perform any task you require, such as formatting data, manipulating strings, or implementing custom business logic.

For example, let’s create a simple helper function called formatPrice that formats a given value as a currency:

<?php

function formatPrice($value)
{
    return '$' . number_format($value, 2);
}

Feel free to add as many custom helper functions as you need within the file.

Step 3: Include the Helper File

To make your custom helper functions available throughout your Laravel application, you need to include the helper file.

Open the composer.json file in your project’s root directory and locate the "autoload" section. Inside this section, add the following line to the "files" array:

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

This informs Composer to autoload the helpers.php file when your application starts up.

Step 4: Run Composer Update

To update the autoloader and make your custom helper functions accessible, open your terminal or command prompt, navigate to your project’s root directory, and run the following command:

composer dump-autoload

This command regenerates the Composer’s autoloader, ensuring that it loads your custom helper file.

Also Read: Laravel Retrieve Random Records from Database Examples

Step 5: Utilize the Custom Helper Functions

Once the autoloader is updated, you can start using your custom helper functions across your Laravel application. They are now globally available, allowing you to invoke them from any class or view.

For example, to use the formatPrice helper function, simply call it in your code like this:

echo formatPrice(25.99); // Output: $25.99

Conclusion:

Custom helper functions in Laravel offer a powerful way to extend the functionality of the framework and simplify common tasks in your application. By following these steps, you can create your own helper functions, organize them in a dedicated file, and make them globally available throughout your Laravel project.

Custom helper functions help improve code reusability, enhance developer productivity, and make your application more maintainable. Embrace the flexibility and convenience they provide and unlock the full potential of your Laravel applications.

Happy coding!

Share this:

Categorized in: