How to create custom helper facade class in laravel 9

How to create custom facade helper in laravel 9, laravel 8, and laravel 7. Facades in laravel in Hindi. Laravel custom helper facade not working. Creating customer helper facade function with example. How to make helper class in laravel.

In this tutorial we will make global functions by help of facade helper class. If can also learn laravel custom helper without helper class.


Laravel helper facade class

In laravel, we create a helper class file in the helpers folder inside the app directory. helper facade directory asapp/Helpers/helpers.php. In simple helper we only created functions but in helper facade create class and functions. In laravel facade we use static function.

app/Helpers/helpers.php
<?php

namespace App\Helpers;
  
class Helper {
  public static function changeToMinutes($time, $type) {
    $minutes = 0;
    if($type == 'hour')
      $minutes = $time * 60;
    elseif($type == 'day')
      $minutes = $time * 60 * 24;

    return $minutes;
  }

  public static function defaultDateFormat($date){
    $newDate = date('d-m-Y', strtotime($date));
    return $newDate;
  }
}

Register laravel custom helper facade

Laravel custom helper facade class register in config/app.php file. open app.php file and register the custom facade in aliases array see below example code.

config/app.php
....

'aliases' => [
  ....
  'Helper' => App\Helpers\Helper::class,
]

Use custom helper facade in blade file

In custom laravel facade we make static function which is use by scope resolution operator with facade class see below blade file to call custom facade.

<!DOCTYPE html>
<html>
<head>
  <title>Helper Facade Example</title>
</head>
<body>

<h2>Call default date by helper facade.</h2>
<p>Date of birth {{ Helper::defaultDateFormat('2000-05-12') }}</p>

</body>
</html>
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links