How to create custom helper in laravel 8 ?

In this tutorial, I will give you an example of the “Custom helper function in laravel“, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

custom helper function in laravel

Need of Custom Helpers in Laravel

Laravel provides a helper function for array, URL, route, path, and blade files. Sometimes we required some important data and we call it from controller or blade files multiple times.

But helper is a better way to reduce the number of code lines and easy to use or easy to access get data anywhere in the entire Laravel application.

In this example, we call all the category table data in the controller and blade file using a custom helper.

get data from helper in laravel

create a folder and a file :

In this step, you need to create app/Helpers/helpers.php in your laravel project and put the following code in that file:

We have to inform the system that we have added a new file to run everywhere in Laravel, so add the “files”: [“app/Helpers/helpers.php”] to composer.json in the autoload block.

Register Helper in composer.json

  "autoload": {
         "files": [

        "psr-4": {
            "App\\": "app/"
        "classmap": [
register helper in laravel

Regenerate the list of all classes in the app, Hit this command in the terminal.

 composer dump-autoload

Now we create a function in Helper\Helpers.php file to get the category data from the database using Category Model.


  namespace App\Helper;
  use App\Model\Category;

  class Helpers
    #Get Category Data From Category Model
    static function get_catdata()
        $catdata = Category::get();
        return $catdata;


That is all, we can call our function anywhere in the view and controller like this:

Get data in blade file using helper

   <div class="container">
         <h3>Category List | Get all the Category using Helper</h3>
         <table class="table">
            $catData = \App\Helper\Helpers::get_catdata()
                  <th>Category Name</th>
               @foreach($catData as $key => $data)
                  <td>{{ $key+1 }}</td>
                  <td>{{ $data->name }}</td>

Get data in controller using helper :


 use App\Http\Controllers\TestController;

 Route::get('/get-cat-data', [TestController::class, 'categoryList']);



  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Helper\Helpers;

  class TestController extends Controller

   public function categoryList()

    $data = Helpers::get_catdata();

Now, you can see the output in the given below images, we have successfully implemented the custom helper function in the Laravel application.

get helper data in controller
get helper data in blade file in laravel

Also Read : Show and Hide Password Field Text using jQuery.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress.

Scroll to Top