Laravel Provider a lot of helper functions for Arrays & Objects, Strings, Fluent Strings, Miscellaneous, and the most used helper function dd().
Step 1: Define Your Helper Functions
To define helpers in laravel create a php file and define a class with methods in itand place the file at app/helpers.php folder in your laravel application.For example
<?php
namespace App\Helpers;
class MyCustomHelper
{
public static function formatDateTime($dateTime)
{
return $dateTime->format('Y-m-d H:i:s');
}
}
Step 2: Autoload Your Helpers File
of any custom helper in composer.json.
Add the following line to the autoload section:
After adding this line, run composer dump-autoload in your terminal to refresh the autoloaded files.
"autoload": {
"files": [ "app/helpers.php" ],"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
Step 3: Use Your Custom Helper Functions
After adding up the helper file in composer.json we need to the following commandcomposer dump-autoload
And afterword we need to use the newly create custom heper in our laravel.For example
use App\Helpers\CustomHelper;
echo CustomHelper::formatDateTime(now());
No comments:
Post a Comment