Laravel 10 Create Custom Helper Function

In this article, we will see laravel 10 create a custom helper function. Here, we will learn about how to create a custom helper function in laravel 10. As we all know laravel provides many in-built helper 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 in laravel 10.

So, Here we will create a custom helper function example in laravel 10. Also, you can see laravel 10 Helpers Function.

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

Laravel includes a variety of global helper PHP functions like Arrays, paths, strings, URLs, etc. So, you can use these functions as per your requirements.

Step 1: 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.php

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

function trimString($string, $repl, $limit) 
{
  if(strlen($string) > $limit) 
  {
    return substr($string, 0, $limit) . $repl; 
  }
  else 
  {
    return $string;
  }
}

function truncate($string, $length)
{
    if (strlen($string) > $length) {
        return substr($string, 0, $length - 3) . '...';
    }
    return $string;
}

?>

 

 

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

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.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>Laravel 10 Create Custom Helper Function - Techsolutionstuff</title>
     <link rel="stylesheet" href="">
 </head>
 <body>
     <h3>Laravel 10 Create Custom Helper Function - Techsolutionstuff</h3>
     <h3>New Date Format: {{ dateFormat('2023-04-02','m/d/Y')  }}</h3>
     <p>Output: {{ trimString("Hello", "...", 3)}}</p>
     <p>Output: {{ truncate("This is a long string", 10) }}
 </body>
</html>

Output: 

New Date Format: 04/02/2023

Output: Hel...

Output: This is ...

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS