Introduction: Why Helper Functions Are a Must-Have in Laravel
\n\nIf 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\nSound 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\nIn 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\nLet's go!
\n\nWhat is a Helper Function and Why Do You Need It?
\n\nA 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.
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
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\nMethod 1: Classic — Manual helpers.php File
\n\nThe simplest and most understandable method for beginners. Create a file app/helpers.php and include it in composer.json.
Step 1. Create the helpers.php file
\n\nGo to the root of your Laravel project and create a file app/helpers.php. Inside, write a simple function:
<?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\nNote the wrapper if (!function_exists(...)). This is a safeguard against conflicts if the function is already defined elsewhere. Always use this construct.
Step 2. Include the file via Composer
\n\nOpen composer.json and in the autoload section add:
"files": [\n "app/helpers.php"\n]\n\nIt should look something like this:
\n\n"autoload": {\n "psr-4": {\n "App\\\\": "app/"\n },\n "files": [\n "app/helpers.php"\n ]\n}\n\nNow run the command in the terminal:
\n\ncomposer dump-autoload\n\nThat's it! Now you can use formatDate() anywhere in your Laravel application — in controllers, models, Blade templates.
Method 2: Via Service Provider (More Advanced)
\n\nThis 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\nStep 1. Create a Service Provider
\n\nRun the artisan command:
\n\nphp artisan make:provider HelperServiceProvider\n\nOpen the created file app/Providers/He