This web article is focused on Laravel 10 custom helper function. we are able to help you to present instances of Laravel 10 custom helpers. you can see Laravel 10 make a custom helper file.
This web article detailed in a way to create a custom helper function in Laravel 10.
Follow the instructions and steps below in order to create the helper functions which are accessible throughout all the PHP files in the project globally.
Step I: Create a helpers.php file inside the app folder
- Create a new file: Create a new PHP file in the app/ directory (if it doesn't exist, you can create it). For example, let's name it CustomHelpers.php.
- Define your helper functions: In the helpers.php file, define your custom helper functions as regular PHP functions. Here's an example:
helpers.php
<?php use \App\Models\Tag; function listPopularTags() { return Tag::orderBy("views", "desc"); } function productImagePath($image_name) { return public_path('images/products/'.$image_name); }
Step II: Adding the path to the composer
Autoload the helper file: Open the composer.json file located in the root directory of your Laravel project. Inside the autoload section, add "files": ["app/helpers.php"] to the files array. It should look like this:
composer.json
... "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" }, "files": [ "app/helpers.php" ] }, ...
Step III: Run the composer dump command
this is the last step, you should just run the following command from your root path in terminal
composer dump-autoload
Ok, now, at last, you can use your custom helper functions like listPopularTags and productImagePath(), I am going to give you an example of how to use custom helper functions, you can use them anywhere in your project blade, controller, route files, etc.
For Example
TestController.php
use \App\Models\Tag; class TestController extends Controller{ public function test(){ return listPopularTags(); } //will return all popular tags }
test.blade.php
<html> <body> <ul> @foreach(listPopularTags() as $tag) <li>{{$tag->title}} </li> @endforeach </ul> </body> </html>
- Run composer update: In your terminal, navigate to your Laravel project's root directory and run the composer update command. This will update the autoloader and make your helper functions available.
- Start using the helper functions: You can now start using your helper functions anywhere in your Laravel application without the need for explicit import statements. For example, you can call formatDate($date) or generateSlug($title) from within your controllers, views, or any other PHP file.
Remember to follow good naming conventions for your helper functions and ensure they don't clash with existing Laravel functions or composer packages.
Note: If you prefer to use a specific namespace for your helper functions, you can define one in your CustomHelpers.php file and then import the namespace wherever you want to use the helper functions.
Conclusion
In conclusion, creating helper functions in Laravel 8 allows you to define custom functions that can be used globally throughout your application without the need for explicit imports or namespaces. By following the steps outlined above, you can create a dedicated helper file, define your helper functions, autoload the file using composer, and start using the helper functions across your Laravel project.
Using helper functions can help simplify code, promote code reusability, and improve the overall maintainability of your application. However, it's important to be mindful of naming conventions and avoid conflicts with existing Laravel functions or composer packages.
With the ability to create helper functions in Laravel, you can streamline your development process and enhance the functionality of your application by encapsulating commonly used code snippets in reusable functions.