How to create custom helper file in Laravel 10?

Feb 24, 2023 . Admin



Hi friends,

Are you looking for an example of laravel 10 to create a custom helper file example?. you will learn How to create custom helper functions in Laravel 10 . you will learn laravel 10 to create a global function. if you have questions about Create own custom helper functions / Classes in Laravel then I will give a simple example with a solution. Follow below tutorial step of going to create a helper function in laravel 10.

today, we are going to create a helper function in laravel 10, this is the same method in creating a helper function in laravel 10. Helper function helps us to create a function that can be called anywhere in our app. That is a global function that can be called both in the views and also 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 also define your own set of helper functions for your Laravel applications and PHP packages, by using 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.

Step 1: Download Laravel
Let 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-app
Step 2 : Add 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-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('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-helper
Output:
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...

#Laravel 10