How to Create Your Own Helper Functions in Laravel

How to Create Your Own Helper Functions in Laravel

Laravel comes with many helper functions out-the-box such as dd(), abort() and session(). Within your app/Http directory, create a helpers.php file and add your functions. Within composer.json , in the autoload block. How to Create Your Own Helper Functions in Laravel.


Helper functions can be extremely useful in your Laravel projects. They can help to simplify your code in your projects in a clean and easy way. Laravel comes with many helper functions out-the-box such as dd()abort() and session(). But as your projects start to grow, you'll likely find that you'll want to add your own.

In this article, we’re going to look at how we can create and use our own custom PHP helper functions in our Laravel projects.

Creating the Helper Function

To add our own helper functions, we need to start by creating a new PHP file to place them in. So, let’s create a helpers.php file inside our app folder. As a side note, this location is down to personal preference really. Another common place I've seen the file placed is inside an app/Helpers/helpers.php file. So, it's down to you to choose a directory that you feel works best for you.

Now, that we’ve got our file, we can add our helper function to it. As a basic example for this article, we’re going to be creating a super-simple function that converts minutes to hours.

Let’s add the function to our helpers.php file like so:


if (! function_exists('seconds_to_hours')) {
    function seconds_to_hours(int $seconds): float
        return $seconds / 3600;

As you can see in the example above, the function itself is really simple. However, one thing that you might notice is that the function name is written in snake case (e.g. seconds_to_hours) rather than camel case (e.g. secondsToHours) like you’d usually see with method names on a class. You aren’t necessarily fixed to using snake case for defining the function name, but you’ll find that all of the Laravel helper functions are written this way. So, I’d probably advise to use the format just so that you can follow the typical standard and format that’s expected. This is totally up to you though.

Another thing that you might have noticed is that we have wrapped the function name inside an ‘if’ statement. This is to stop us from accidentally redeclaring any helpers with the same name that have already been registered. For example, let’s say that we were using a package that already had a seconds_to_hours() function registered, this would prevent us from registering our own function with the same name. To get around this, we could just simply change the name of our own function to avoid any clashes.

It’s also really important to remember when creating helper functions that they are only supposed to be used as helpers. They aren’t really meant to be used to perform any business logic, but rather to help you tidy up your code. Of course, you can add complex logic to them, but if this is something you’re looking to do, I’d probably advise thinking about if the code would be a better fit in another place such as a service class, action class or trait.

laravel php coding

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

Bar Code Generator In Laravel 7.x

Laravel 7.x, 6 QR/Bar code generate. Here you will learn, how you can generate QR/Bar codes in laravel projects. Also sent in it on mail/SMS.

Laravel Vs CodeIgniter (Choosing Best PHP Framework In 2021)

![This is image title]( "This is image title") Do you want to create an outstanding web application? You would need a flexible and scalable framework in terms...

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→

Login Page in PHP with Database Source Code

Simple login form in PHP using session. Here you'll learn how to create complete login form and authenticate users from MySQL database in PHP.