How To Create Custom Helper Function In Laravel 10

    In this article, we will see how to create a custom helper function in laravel 10. Here we will learn about laravel 10 to create a custom helper function example. As we all know laravel provides many inbuilt functions in their framework, but many times we need to require our own customized function to use in our project that time we need to create a custom helper function.

    Laravel 10 also provides helper functions for arrays and objects, paths, strings, URLs, routes, etc.

    So, let's see laravel 10 creates a custom helper function, create a custom helper in laravel 10, laravel custom helper, and how to use the helper function in laravel 10.

Step 1: Install Laravel 10

    In this step, we will install laravel 10 using the following command.

composer create-project laravel/laravel laravel_10_custom_helper
Step 2: Create helpers.php File

    In this step, we will create the helpers.php file in our laravel project and add the below code.

    app/Helpers/helpers.php

<?php

use Carbon\Carbon;
  
function change_Date_Format($date,$format){
    return \Carbon\Carbon::createFromFormat('Y-m-d', $date)->format($format);    
}

?>
Read Also: Laravel 9 to_route() and str() Helper Function
Step 3: Add Helper File Path In composer.json File

    In this step, we will add the below code in the composer.json file.

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
         "files": [
             "app/Helpers/helpers.php"
        ]
    },
Step 3: Run Command

    Now, run the below command in your terminal

composer dump-autoload

     So, we are done with the custom helper function in laravel 10, as of now we can use this function anywhere across applications.

    Here, we will use this function in the blade file.

<html>
 <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title>How To Create Custom Helper Function In Laravel 10 - Websolutionstuff</title>
     <link rel="stylesheet" href="">
 </head>
 <body>
    <h3>How To Create Custom Helper Function In Laravel 10 - Websolutionstuff</h3>
     <h3>New Date Format: {{ change_Date_Format('2023-03-03','m/d/Y')  }}</h3>
 </body>
</html>

    Output:

New Date Format: 03/03/2023

    You might also like:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan