Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

get helper function data in blade file in laravel

Call the helper function in the blade file in Laravel 8

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

Need to Call the helper function in the blade file 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 and we call the helper function data in Laravel.

Recommended Article:- How to create a custom helper in laravel 8.

Let’s call the helper function data in the blade file in Laravel.

We have a categories table in our database, and we have some records, we get all the records in our helper.php file and show them on the blade file.

helper function in laravel

app\Helper\Helpers.php

 <?php

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

  class Helpers
  {
    #Get category Data
    static function get_catdata()
    {
       $catdata = Category::get();
       return $catdata;
    }
  }

  ?>

Get data in the blade file using helper

resources\views\category\list.blade.php

   <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>
call helper file in the blade file in laravel

In this article, we learned “How to Call the helper function in the blade file in Laravel 8”, I hope this article will help you with your Laravel application Project.

Also Read:- Handle multiple pages using a single route in Laravel.