How To Create Custom Helper Function In Laravel 10

Websolutionstuff | Mar-20-2023 | Categories : Laravel

In this article, we'll explore how to create a custom helper function in Laravel 10. This custom helper function can be a powerful tool to simplify and streamline your Laravel applications. While Laravel offers numerous built-in functions, there are situations where you may need to craft your own customized helper function tailored to your project's specific needs.

Laravel 10 continues to provide handy helper functions for a wide range of tasks, including working with arrays, objects, paths, strings, URLs, and routes.

By the end of this article, you'll not only know how to create custom helpers in Laravel 10 but also how to effectively integrate them into your projects for enhanced functionality and efficiency.

Let's dive into the world of custom helpers in Laravel 10 and unlock their potential

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);    
}

?>

 

 

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

 

Conclusion:

In conclusion, we've learned how to create custom helper functions in Laravel 10. These custom helpers act as handy tools that simplify and optimize your Laravel projects.

While Laravel provides a variety of built-in functions, there are scenarios where having your own customized helper can be a game-changer.

 


You might also like:

Recommended Post
Featured Post
Laravel 9 Livewire Toastr Notification
Laravel 9 Livewire Toastr Noti...

In this article, we will see laravel 9 livewire toastr notification. Here, we will learn how to create toastr notif...

Read More

Nov-28-2022

How To Downgrade PHP 8 to 7.4 in Ubuntu
How To Downgrade PHP 8 to 7.4...

As a web developer, I understand the significance of embracing the latest technologies to stay ahead in the dynamic worl...

Read More

Aug-04-2023

How To Create Calendar Event In Laravel 9 Using AJAX
How To Create Calendar Event I...

In this article, we will see how to create a calendar event in laravel 9 using ajax. Here, we will learn how to add...

Read More

Dec-28-2022

How To Create Unique Slug In Laravel 9
How To Create Unique Slug In L...

In this article, we will see how to create a unique slug in laravel 9. A slug is the part of a URL that i...

Read More

Sep-27-2022