A Helper function is a small piece of code that performs a specific task or operation and can be used anywhere in your application.
In Laravel you need to create a new PHP file to write Helper functions and specify the file path in the composer.json file.
In this article, you will learn how to create Helper functions in Laravel and how to use it.
Contents
- Create Helper Class
- Update config/app.php
- Update Composer
- Calling Helper function in Controller
- Calling Helper function in View
- Conclusion
1. Create Helper Class
- Create
Helpers.php
file inapp/
folder. - You can access Model, Auth, etc. from this file.
- I am including
Employees
Model –use App\Models\Employees;
. - Create 2 static methods –
- helperfunction1() – Return simple text.
- getEmployeeStatus() – This method takes 1 parameter – employee id. Fetch record by
$id
fromemployees
table and return status –return $record->status;
.
Completed Code
<?php namespace App\Helpers; use App\Models\Employees; class Helper { public static function helperfunction1(){ return "helper function 1 response"; } public static function getEmployeeStatus($id=0){ $record = Employees::find($id); return $record->status; } }
2. Update config/app.php
- Open
config/app.php
file. - Add
'Helper' => App\Helpers\Helper::class,
inaliases
.
'aliases' => Facade::defaultAliases()->merge([ // .... 'Helper' => App\Helpers\Helper::class, ])->toArray(),
3. Update Composer
- Open
composer.json
file. - Specify
Helpers.php
file inautoload
–
"autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }, "files": [ "app/Helpers.php" ] },
- Update autoload files –
composer dump-autoload
4. Calling Helper function in Controller
- Create
PagesController
controller –
php artisan make:controller PageController
- To access Helper function need to include it using –
use Helper;
.
Call Helper function in Controller –
Helper::functionname();
Completed Code
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Helper; class PagesController extends Controller { public function index() { // Calling $helperfunction1_res = Helper::helperfunction1(); $employeeStatus = Helper::getEmployeeStatus(2); // Return 0 or 1 // Print return response echo "<b>helperfunction1() response -</b> ".$helperfunction1_res."<br>"; echo "<b>getEmployeeStatus() response -</b> ".$employeeStatus; die; } }
Output –
helperfunction1() response - helper function 1 response getEmployeeStatus() response - 1
5. Calling Helper function in View
Calling Helper function in view is similar to the controller but does not require to include Helper Class.
Call Helper function in View –
Helper::functionname();
Completed Code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>How to create Helper function and use it in Laravel</title> </head> <body> @php // Calling $helperfunction1_res = Helper::helperfunction1(); $employeeStatus = Helper::getEmployeeStatus(2); // Return 0 or 1 // Print return response echo "<b>helperfunction1() response -</b> ".$helperfunction1_res."<br>"; echo "<b>getEmployeeStatus() response -</b> ".$employeeStatus; @endphp </body> </html>
Output –
Output will be the same as the controller.
helperfunction1() response - helper function 1 response getEmployeeStatus() response - 1
6. Conclusion
In the example, I have created Helper class and defined methods in it but you can also create functions without the class.
Define the functions that you want to access globally in the project.
Make sure to run composer dump-autoload
after specifying the file path in composer otherwise, you wouldn’t be able to access Helper functions.