custom helper function in laravel 8

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": [
            "app/Helper/Helpers.php"
        ],

        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
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.

app\Helper\Helpers.php

  <?php
  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

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

Get data in controller using helper :

routes\web.php

 use App\Http\Controllers\TestController;

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

App\Http\Controllers\TestController

  <?php

  namespace App\Http\Controllers;

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

  class TestController extends Controller
  {

   public function categoryList()
   {

    $data = Helpers::get_catdata();
    dd($data);
   }
 }

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.