ItSolutionStuff.com

How to Create Custom Helper Function in Laravel?

By Hardik Savani β€’ November 5, 2023
Laravel

Hi Friends,

In this comprehensive tutorial, we will learn laravel create custom helper functions. you'll learn laravel create global function. you'll learn laravel create custom helper file. let’s discuss about laravel create global helper file.

we know Laravel also provides helper functions for array, URL, route, path, etc. but sometimes we may require more custom helper functions for our project. so we need to create our own custom helper file and define global functions that can easily use it.

Here, I will give you a few steps to show you how to create a custom helper function in laravel.

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 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:

app/Helpers/helpers.php

<?php

use Carbon\Carbon;

/**

* Write code on Method

*

* @return response()

*/

if (! function_exists('convertYmdToMdy')) {

function convertYmdToMdy($date)

{

return Carbon::createFromFormat('Y-m-d', $date)->format('m-d-Y');

}

}

/**

* Write code on Method

*

* @return response()

*/

if (! function_exists('convertMdyToYmd')) {

function convertMdyToYmd($date)

{

return Carbon::createFromFormat('m-d-Y', $date)->format('Y-m-d');

}

}

Step 3: Register File Path In composer.json File

In this step, you have to put path of helpers file,so basically open composer.json file and put 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 composer auto load command so that will load our helper file.

next run bellow command:

composer dump-autoload

Step 4: Add 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('call-helper', function(){

$mdY = convertYmdToMdy('2022-02-12');

var_dump("Converted into 'MDY': " . $mdY);

$ymd = convertMdyToYmd('02-12-2022');

var_dump("Converted into 'YMD': " . $ymd);

});

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/call-helper

Output:

string(32) "Converted into 'MDY': 02-12-2022"

string(32) "Converted into 'YMD': 2022-02-12"

Use In Blade File:

You can also use in blade file as like bellow:

<p>Date: {{ convertYmdToMdy('2022-02-12') }}</p>

<p>Date: {{ convertMdyToYmd('02-12-2022') }}</p>

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Add Extra Field in Registration Form in Laravel?

Read Now β†’
β˜…

Laravel Passwordless Login with Magic Link Tutorial

Read Now β†’
β˜…

How to Remove HTML Tags from String in Laravel?

Read Now β†’
β˜…

Laravel TCPDF: Generate HTML to PDF File Example

Read Now β†’
β˜…

How to Take Browser Screenshots in Laravel?

Read Now β†’
β˜…

Laravel Mail Send with PDF Attachment Example

Read Now β†’
β˜…

How to set CC And BCC Email Address In Laravel Mail?

Read Now β†’
β˜…

How to Update Multiple Records in Laravel?

Read Now β†’
β˜…

How to Get Browser Name and Version in Laravel?

Read Now β†’
β˜…

How to Use Google Translator in Laravel?

Read Now β†’
β˜…

Laravel Create Custom Artisan Command with Example

Read Now β†’
β˜…

Laravel Create Custom Error Page Example

Read Now β†’
β˜…

How to Make Custom Middleware in Laravel?

Read Now β†’