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 How to convert html to pdf in laravel 8 9

How to convert html to pdf in laravel 8 / 9?

May 5, 2022November 6, 2023

Dompdf package has rich features to convert html to PDF in laravel. In this article we will learn to use html to PDF using dompdf package. In most of the website there is need of to convert html to PDF or export the data in PDF format, Dompdf does this…

Read More
Php How to use conditional where in Laravel 8

How to use conditional where clause in Laravel 8 eloquent ?

February 7, 2022October 4, 2022

In Laravel sometimes while creating a eloquent or db builder query you wanted to apply the where on basis of some conditions and to achieve it you may use if else condition but laravel 8 itself provides solution to handle such type of situations using when method. Laravel eloquent when…

Read More
Php How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do…

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

  • July 2025
  • 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

  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version