How to Create Your Own Helper Functions in Laravel: A Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Helper Functions Are a Must-Have in Laravel

\n\n

If you've ever written code in Laravel, you've likely encountered this situation: in a controller or template, you need to repeatedly format a date, truncate text, check permissions, or generate a slug. You copy the same piece of code from project to project, and then curse yourself for the duplication.

\n\n

Sound familiar? Welcome to the club. On Stack Overflow, the question "How to create custom helper functions in Laravel" gathers thousands of views — and for good reason. Helper functions (or simply "helpers") are your way to say "no" to copy-paste and "yes" to clean, reusable code.

\n\n

In this tutorial, we won't just show you how to create your first helper function. We'll cover three proven methods — from the simplest to an advanced one with autoloading via Composer. You'll learn how to organize helpers so they don't turn into a dumping ground, and how to test them without a headache.

\n\n

Let's go!

\n\n

What is a Helper Function and Why Do You Need It?

\n\n

A helper function is a regular PHP function that performs one specific task. Laravel already has built-in helpers: str_slug(), route(), config(), and others. But sooner or later, you'll need something of your own — for example, a function for formatting a phone number or generating a unique identifier.

\n\n

Benefits of custom helpers:

\n
    \n
  • Code Reusability — write once, use everywhere.
  • \n
  • Readability — instead of 10 lines with conditions, you write formatPhone($number).
  • \n
  • Testability — helpers are easy to cover with unit tests.
  • \n
  • Centralized Management — change the logic in one place, and it applies throughout the entire project.
  • \n
\n\n

But there is a pitfall: improper organization of helpers can turn your project into chaos. So let's do it right from the start.

\n\n

Method 1: Classic — Manual helpers.php File

\n\n

The simplest and most understandable method for beginners. Create a file app/helpers.php and include it in composer.json.

\n\n

Step 1. Create the helpers.php file

\n\n

Go to the root of your Laravel project and create a file app/helpers.php. Inside, write a simple function:

\n\n
<?php\n\nif (!function_exists('formatDate')) {\n    function formatDate($date, $format = 'd.m.Y')\n    {\n        if (!$date) {\n            return null;\n        }\n        return Carbon\\Carbon::parse($date)->format($format);\n    }\n}
\n\n

Note the wrapper if (!function_exists(...)). This is a safeguard against conflicts if the function is already defined elsewhere. Always use this construct.

\n\n

Step 2. Include the file via Composer

\n\n

Open composer.json and in the autoload section add:

\n\n
"files": [\n    "app/helpers.php"\n]
\n\n

It should look something like this:

\n\n
"autoload": {\n    "psr-4": {\n        "App\\\\": "app/"\n    },\n    "files": [\n        "app/helpers.php"\n    ]\n}
\n\n

Now run the command in the terminal:

\n\n
composer dump-autoload
\n\n

That's it! Now you can use formatDate() anywhere in your Laravel application — in controllers, models, Blade templates.

\n\n

Method 2: Via Service Provider (More Advanced)

\n\n

This method is suitable if you want strict control over when and which helpers are loaded. For example, if you have helpers only for the admin panel or for the API.

\n\n

Step 1. Create a Service Provider

\n\n

Run the artisan command:

\n\n
php artisan make:provider HelperServiceProvider
\n\n

Open the created file app/Providers/He

Blogs

Book Recommendations