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

Helpers function is the most important when it comes in big projects, Laravel has a feature that we can register our custom helpers function which is available globally in our project, we need to register our helper in composer and dump it, follow..

Admin in Internet & Tech 11 months 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

Comments (3)

Sarabah Khan 11 months ago

Change any use statements from FilamentCurator to Awcodes\Curator.
Change FilamentCurator\Forms\Components\MediaPicker fields to Awcodes\Curator\Components\Forms\CuratorPicker.

Samir 11 months ago

How do i get the asserters for the instances to provide the data at the end of the fragment

can we do it using javascript?

Nimesh Doe 4 months ago

Explore the depths of knowledge with NirBytes, a website/blog dedicated to enriching your mind and expanding your horizons. Our platform is a gateway to a vast array of information, insights, and inspiration, curated to stimulate intellectual curiosity and foster lifelong learning.


Hello There
Read next

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

In this blog post, we explore the process of creating helper functions in Laravel 8, enabling developers to streamline their devel

Admin 11 months ago · 3 min read

Related Posts

How to Connect WiFi Without Password

Are you trying to connect to a wifi network but don't know the password? If so, don't worry! There are some simple hacks that can ...

Admin · 11 months ago · 6 min read

How to Root Android Phone | Root Any Phone Without PC (2023)

we will walk you through the step-by-step process of rooting any Android phone without using a PC....

Admin · 9 months ago · 7 min read

How to See Vault Hidden Images Without Password

Are you trying to view hidden images stored in your Vault app but don't have the password? Don't worry, there is still a way for y...

Admin · 11 months ago · 6 min read

How to Recover Deleted Messages on Messenger | Recover Deleted Messages on Facebook (2023)

Recovering deleted messages from Facebook Messenger can be challenging because once a message is deleted, it is generally no longe...

Admin · 11 months ago · 8 min read

How to Recover Hacked Facebook Account Without Email and Phone Number

Are you trying to recover your hacked Facebook account but don't have access to the associated email address or phone number? Don'...

Admin · 11 months ago · 7 min read

Passing Dynamic Attributes to Components Without Database Query

The Gmail dot trick is a technique that allows you to manipulate the placement of dots within your Gmail address without changing ...

Admin · 11 months ago · 5 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!