Laravel 9 Create Global Functions Tutorial
Feb 12, 2022 . Admin
Hi Dev,
Today, In this example how to create a custom helper function example. This post will give you a simple example of How to create custom helper functions in Laravel 9. Here, we will implement a laravel 9 creating a global function. we'll go through Create own custom helper functions / Classes in Laravel.
So, we are going to scratch from engender a helper function in laravel 9, this is identically tantamount method in engendering a helper function in laravel 9. Helper function avails us to engender a function that can be called anywhere in our app. That is an ecumenical function that can be called both in the views and additionally in the controller.
Laravel provides many excellent helper functions that are convenient for doing things like working with arrays, file paths, strings, and routes, among other things like the beloved dd() function.
You can withal define your own set of helper functions for your Laravel applications and PHP packages, by utilizing Composer to import them automatically.
In laravel custom helper, you can create your own function and call anywhere like route, blade view, models, controller etc in laravel project. It is best practice to code reusable and saves a lot of time to replicate the code.
So let's start by following an example.
Step 1: Download LaravelLet us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-appStep 2 : Create helpers.php File
In this step, you need to create app/Helpers/helpers.php in your laravel project and put the following code in that file:
<?php use Carbon\Carbon; /** * Write code on Method * * @return response() */ if (! function_exists('convertDmyToYmd')) { function convertDmyToYmd($date) { return Carbon::createFromFormat('d-m-Y', $date)->format('Y-m-d'); } } /** * Write code on Method * * @return response() */ if (! function_exists('convertYdmToMdy')) { function convertYdmToMdy($date) { return Carbon::createFromFormat('Y-d-m', $date)->format('m-d-Y'); } }Step 3: Register File Path In composer.json File
In this step, you have to put the path of the helpers file, so basically open the composer.json file and put the following code in that file:
composer.json... "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }, "files": [ "app/Helpers/helpers.php" ] }, ...
After register, we need to run the composer auto load command so that will load our helper file.
Then after run bellow command:
composer dump-autoloadStep 4: Create Route
Next, You have to open and update the following routes in the routes/web.php file.
routes/web.php<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('run-helper', function(){ $ymd = convertDmyToYmd('02-12-2022'); var_dump("Converted into 'YMD': " . $ymd); echo "<br>"; $mdy = convertYdmToMdy('2022-02-12'); var_dump("Converted into 'MDY': " . $mdy); });Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/run-helperOutput:
string(32) "Converted into 'YMD': 2022-12-02" string(32) "Converted into 'MDY': 12-02-2022"Use In Blade File:
You can also use in blade file as like bellow:
Date: {{ convertDmyToYmd('2022-02-12') }}
Date: {{ convertYdmToMdy('02-12-2022') }}
I hope it can help you...