Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Custom helper function laravel

How to Create Custom Helper Functions in Laravel ?

Aman Jain, December 21, 2021November 8, 2023

Sometimes in our application we want to create custom helper functions in laravel to use in our application, Laravel has so many helpers functions to work with strings, URL, email, array, debugging etc. so in this article i will show you to use custom helper functions in our Laravel application.

Create Custom Helper Functions in Laravel Example

Examples will work with any verison of laravel like laravel 5, laravel 6, laravel 7 , laravel 8 ,laravel 9 and laravel 10 too.

Let’s begin the tutorial of Create Custom Own Helper Functions step by step

Step 1 : Create a helper file

First step is create a helper file in app folder or we can create a folder in app folder then we can create multiple files in that folder. in this example i am going to create a file in app/helpers.php

<?php 

function myTestFunction(){

    return "this is a simple test function from readerstacks.com";

}

Step 2 : Register helper in composer.json

To auto load our helpers.php we need to register it in our composer.json file. Generally in core PHP we include our new file and then we can directly use that in any file but now we have much better way to include it in our application using the composer as below.

"autoload": {
    "files": [
        "app/helpers.php"
    ],
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

As you can se we have added

 "files": [
        "app/helpers.php"
    ],

in composer.json, composer has a files key which accepts array and where you can define your custom files.

then run the below command in terminal to update the autoloader

composer dump-autoload
Screenshot 2021 12 21 at 7.59.40 AM

Step 3 : Create Routes

Now, Create a route to check our implementation on controller or route itself using anonymous function.

<?php

use Illuminate\Support\Facades\Route;
 
Route::get('/helper',function(){
   return myTestFunction();
}); 

Or in controller

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

 
Route::get('/helper',[ArticleController::class, 'helper']); 

Create a controller in app\Http\Controllers\ArticleController and method helper

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    public function helperTest(Request $request)
    {
        return myTestFunction();
    }
}
?>

Step 4: Example to use in view

Same as controller and route we can use the custom helpers in view blade file, so create a file in resources/views folder

 <div class="row jsc">
           <h2>
           {{myTestFunction()}}
          </h2>
 </div>

and in controller or route

<?php

use Illuminate\Support\Facades\Route;
 
Route::get('/helper',function(){
   return view("helper");
}); 
Screenshot 2021 12 21 at 8.08.15 AM
Custom Laravel helper example

Also Read : Create custom class or library class in Laravel 8

Related

Php Laravel helperlaravelphp

Post navigation

Previous post
Next post

Related Posts

Php Laravel sum in eloquent query

Laravel sum in eloquent query with example

January 23, 2022November 10, 2023

In this article we will learn to use aggregate function sum in laravel eloquent and query builder. Laravel itself provide inbuilt method to sum the columns like in MySQL or SQL based database we can do sum of columns using sum aggregate function. Sum method will give you the sum…

Read More

How to enable cors in web api php or laravel?

August 27, 2021August 20, 2022

Cross-Origin Resource Sharing (CORS) is mechanism which allow browser to share the resources between the other domain or port. if the cors is disabled in api or server then other domain can’t access the apis and resource of server. Example: if a server A wants to access the apis of server…

Read More
Php How to Share URL on social media using package in Laravel 98765

How to Share URL on social media using package in Laravel 9/8/7/6/5 ?

March 15, 2022October 4, 2022

In this tutorial i will show you to Share URL on social media using package in Laravel. Laravel provides several packages to make the task easy, In the same way we can add third party package to share the URLs on multiple social media using the package jorenvanhocht/laravel-share. This package…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version