How to Create Helpers Function in Laravel 8 (Global Function)

By Nir Bytes · Updated 6 days ago · 3 min read

custom helper function in laravel 10

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

  1. 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.
  2. 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>


  1. 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.
  2. 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.





Share this


Related Posts

10 Best Website Unblockers for School Chromebook!

In this article, we will explore the top 10 website unblockers specifically tailored for school Chromebook users....

Nir Bytes · 2 months ago · 5 min read

How to Unblock Blocked Websites on Google Chrome 20.25

We are here to help you unblock blocked websites on Google Chrome in the year 2025 and achieve uninterrupted access to the content...

Nir Bytes · 2 months ago · 5 min read

How To See WiFi Password in Mobile

In this comprehensive guide, we will walk you through the steps to see WiFi passwords on your mobile devices. We understand how fr...

Nir Bytes · 2 months ago · 5 min read

BBS 3rd Year Business Environment and Strategy Model Question

Here is the Model Question for BBS 3rd Year Business Environment and Strategy &nbsp; 2079(2023). it provides a set of sample quest...

Nir Bytes · 2 months ago · 2 min read

Check BBS result

BBS Result 2080 1st 2nd 3rd 4th year Tribhuvan University results can be checked from the official website of the university. The ...

Nir Bytes · 2 months ago · 4 min read

BBS 3rd Year Foundation of Financial Systems Model Question 2079(2023)

Here is the Model Question for BBS 3rd Year Foundation of Financial Systems 2079(2023). it provides a set of sample questions for ...

Nir Bytes · 2 months ago · 3 min read
Become a member
Get the latest news right in your inbox. It's free and you can unsubscribe at any time. We hate spam as much as we do, so we never spam!