To start, I created a folder in my application directory called Helpers . Then, in the Helpers folder, I added files for the features I wanted to add. Having a folder with several files avoids the fact that one large file becomes too long and unmanageable.
Then I created HelperServiceProvider.php by running the artisan command:
artisan make:provider HelperServiceProvider In the register method, I added this fragment
public function register() { foreach (glob(app_path().'/Helpers/*.php') as $filename){ require_once($filename); } }
finally register the service provider in config/app.php in the providers array
'providers' => [ 'App\Providers\HelperServiceProvider', ]
After that, you need to run composer dump-autoload and your changes will be visible in Laravel.
any file in the Helpers directory is now loaded and ready to use.
Hope it works!
source share