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 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
Php Laravel 11 Ajax Image Upload with form with example

Laravel 11 Ajax Image Upload with form with example

July 16, 2024July 16, 2024

In this tutorial, I will demonstrate how to implement Laravel 11 Ajax Image Upload with form. This can be achieved using Laravel’s file and storage providers. In our previous articles, such as How to Upload image in Laravel 11 ? we explained how to upload images without Ajax. However, today…

Read More
Php Simplest Way to Use Roles and Permission in Laravel

Simplest Way to Use Roles and Permission in Laravel

August 23, 2022March 16, 2024

Roles and permission in laravel are used to provide roles based authentication and in this post we will learn simplest wat to use roles and permission in laravel. In our application we want features and modules on basis of roles or permissions so that we can restrict users to access…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version