DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

Laravel 8 Create Custom Helper Function Example

In this tutorial we will see laravel 8 create custom helper function example. As we all know laravel provides many in-built function in their framework, but many times we need to require our own customised function to use in our project at that time we need to create custom helper function. So, here I am show you custom helper function example in laravel 8.

You can create own custom helper function in laravel 6, laravel 7 and larvel 8. Laravel includes a variety of global "helper" PHP functions.

Arrays & Objects

Arr::first
Arr::get
Arr::pluck
Arr::prepend
Arr::sort
Enter fullscreen mode Exit fullscreen mode

Paths

config_path
public_path
storage_path
Enter fullscreen mode Exit fullscreen mode

Strings

Str::replace
Str::slug
Str::before
Str::limit
Enter fullscreen mode Exit fullscreen mode

Fluent Strings

isEmpty
trim
dirname
explode
Enter fullscreen mode Exit fullscreen mode

URLs

asset
route
url
Enter fullscreen mode Exit fullscreen mode

Miscellaneous

config
dd
env
with
collect
csrf_token
Enter fullscreen mode Exit fullscreen mode

So, let's see how to create custom helper in laravel 8.

Step 1 : Create helpers.php File

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

app/helpers.php

<?php

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

?>
Enter fullscreen mode Exit fullscreen mode

Step 2: Add Helper File Path In composer.json File

Add below code in composer.json file.

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
         "files": [
            "app/helpers.php"
        ]
    },
Enter fullscreen mode Exit fullscreen mode

Step 3: Run Command

Now, run below command in your terminal

composer dump-autoload
Enter fullscreen mode Exit fullscreen mode

So, we are done with custom helper function in laravel 8, as of now we can use this function any where.

Here i am using this function in blade file.

<html>
 <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title>Laravel 8 Create Custom Helper Functions Example - Techsolutionstuff</title>
     <link rel="stylesheet" href="">
 </head>
 <body>
    <h3>Laravel 8 Create Custom Helper Functions Example - Techsolutionstuff</h3>
     <h3>New Date Format: {{ change_Date_Format(date('Y-m-d'),'m/d/Y')  }}</h3>
 </body>
</html>
Enter fullscreen mode Exit fullscreen mode

You might also like :

Discussion (0)