Laravel 10 Create Custom Helper Function Example

  • 26-02-2023
  • 824
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In this post, we will discuss how to make the laravel custom helper function in laravel 10, Laravel custom helper function means we can create our custom-made function and add it to the helper file so that we can use that custom made function from the helper function.

To use a custom-made function in laravel we just need to create a helper function, in this post I have given all guidelines for creating a helper function in laravel 10 how we can create and how to register that help function made in the composer.json file.

Making a helper function in laravel 10 is similar to an older version, Beginner developers who start learning laravel 10 an easy to understand how to make the guide here in this post given from scratch step by step. just follow it.

Step 1. Create Helper File

We will create a php file in the app/Helpers/helper.php path.

Go to the app folder and create the "Helper" folder, after in this folder open and create a "helper.php" file. In this file, we will start php, don't need to over php.

In this file we will define two functions, The first function we will return a simple string. In the second function, we will pass a parameter and we will return that value. Here in this post, I am taking just an understanding example. you can create a per your requirement.

app/Helpers/helper.php
<?php

function myCustomFunction()
{
    return 'Do you Logic Here';
}

function myCustomFunction1($name)
{
    return $name;
}

Step 2. Add Helper Path

Once we create a helper.php file in the given path, we have to define it now in the composer.json file. we will create a "files array attribute" in the "psr-4" object and then we will add that create file path. you can see the below example.

composer.json
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files":[
        "app/Helpers/helper.php"   // THIS PATH
    ]
},

Step 3. Composer Autoload

After we added the full path in the composer.json file. we will autoload the composer file for the execute that helper function file that we create with the system files.

So that we will run a command below suggested :

composer dump-autoload

It will take some time for dumping all the project and files of the project.

Step 4. Output

We will dump the first function and it will return the given inside function string. In the second function, we will pass an argument, and that function will return that function provided argument and return.

dd(myCustomFunction());

// "Do you Logic Here"
dd(myCustomFunction1('My custom helper function'));

// "My custom helper function"
// IN BLADE FILE

{{ myCustomFunction1('My custom helper function') }}
// IF YOUR FUNCTION VALUE IS HTML

{!! myCustomFunction1('<p>My custom helper function</p>') !!}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :