Skip to main content

Command Palette

Search for a command to run...

Laravel 12 Create Custom Helper Function

How to Create Custom Helper Function in Laravel

Updated
3 min read
Laravel 12 Create Custom Helper Function
I

Experienced Web Developer | Laravel | PHP | jQuery | HTML | CSS I’m a skilled web developer with extensive experience building dynamic and responsive web applications using Laravel, PHP, jQuery, HTML, and CSS. I specialize in creating clean, scalable, and maintainable code, with a focus on performance and user experience. Whether it's developing RESTful APIs, building custom CMS features, or crafting responsive front-ends, I bring a problem-solving mindset and a passion for quality to every project.

In this guide, you’ll learn how to create and use custom helper functions in Laravel 12 with a clear step-by-step approach.

Helper functions in Laravel are small, globally available PHP methods that make it easier to perform repetitive or common tasks. They improve code readability and reduce duplication since you can call them directly from anywhere in your application—whether it’s a controller, view, middleware, or even a service provider.

By default, Laravel ships with many built-in helper functions. But you can also define your own custom helpers to extend the functionality. To make them accessible throughout your project, simply include the helper file in your composer.json, ensuring it’s loaded automatically.

Read Also : Laravel 12 Summernote Image Upload CRUD Example

Some useful built-in helper functions in Laravel include:

  • url() : Generate a complete URL for a specific path

  • route() : Get the URL for a named route

  • asset() : Create a URL for public assets like CSS/JS files

  • config() : Fetch values from configuration files

  • dd() : Dump variables and stop execution (very helpful for debugging)

Helpers cover many areas such as string manipulation, array handling, URL generation, and debugging. Creating custom ones allows you to tailor solutions specific to your application’s needs.

Steps for Laravel 12 Create Custom Helper Function

  • Step 1: Install Laravel 12
  • Step 2: Create a helpers File
  • Step 3: Autoloading helper.php via composer.json
  • Step 4: Dump Autoload Files
  • Step 5: Use Your Helper Function

Step 1: Install Laravel 12

First, make sure you have a fresh Laravel project to work with. If one is already set up, feel free to skip ahead. Otherwise, you can create a new Laravel application by running this command in your terminal:

composer create-project laravel/laravel laravel-custom-helper-demo

Once the installation completes, you’re ready to move on.

Step 2: Create a Helper File

Next, let’s add a dedicated file for our custom helper functions. In this example, we’ll place a new file called helpers.php inside the app/Http directory. (You can store it anywhere in your project structure, but this is a common location.)

app/Http/helpers.php

<?php

use Carbon\Carbon;

if(!function_exists('formatDate')){
    function formatDate($date)
    {
        return Carbon::parse($date)->format('Y M d');
    }
}

if(!function_exists('formatText')){
    function formatText($text)
    {
        return strtoupper($text);
    }
}

Laravel makes use of function_exists checks to ensure the same helper function isn’t declared multiple times. Including these checks is a good practice—it prevents PHP errors and keeps your codebase safe from naming conflicts.

While it’s rare, collisions can happen if two different packages or files use the same function name. To reduce the chance of this, you can adopt unique naming patterns, like adding a prefix to your helper function names.

Check out full tutorial