How to add custom functions to Laravel?

1)You will need to create a new folder called “CustomFunctions” in app/

This will create a new folder where all your controller folder are already in.

2)Create a PHP file inside this folder called:

CustomFunctions.php

Add this code in it(function example):


3)Add a path to this file inside your composer.json file as:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/CustomFunctions/CustomFunctions.php"   <-------here
        ]
    },

Make sure you add the "files" inside the "autoload" or you will get issues.

4)Save all your files and run this command:

composer dump-autoload

Now try to test this function inside any of your controllers, it should work as expected.

Simple!

Enjoy

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Disclaimer:

As an Amazon Associate I earn from qualifying purchases. This post may contain affiliate links which means I may receive a commission for purchases made through links.