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

Laravel How to Find the laravel version and location

How to find installed laravel version?

March 5, 2022March 5, 2022

To find the installed laravel version in command line we can use artisan command and to find the laravel version in application code we can use app method. In this article i will show you to get the version of laravel version in both ways using command line and in…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

Read More
Php How to print raw sql query in eloquent laravel

How to print raw sql query in eloquent laravel ?

November 23, 2022March 16, 2024

In this article we will learn to print raw sql query in eloquent laravel. Sometime to debug the MySQL query in laravel eloquent we need to echo raw sql query and want to know exact query. In laravel we have multiple methods to print database query in laravel 8 and…

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