Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4

Laravel Custom Helper function

From WikiOD

Adding custom helpers can assist you with your development speed. There are a few things to take into consideration while writing such helper functions though, hence this tutorial.

Remarks[edit | edit source]

Just a few pointers:

  • We've put the function definitions within a check (function_exists) to prevent exceptions when the service provider is called twice.
  • An alternative way is registering the helpers file from the composer.json file. You can copy the logic from the laravel framework itself.

HelpersServiceProvider.php[edit | edit source]

Now let's create a service provider. Let's put it under app/Providers:


namespace App\Providers;

class HelpersServiceProvider extends ServiceProvider
    public function register()
        require_once __DIR__ . '/../Helpers/document.php';

The above service provider load the helpers file and registers your custom function automatically. Please make sure you register this HelpersServiceProvider in your config/app.php under providers:

'providers' => [
     // [..] other providers

document.php[edit | edit source]


if (!function_exists('document')) {
    function document($text = '') {
        return $text;

Create a helpers.php file, let's assume for now it lives in app/Helpers/document.php. You can put many helpers in one file (this is how Laravel does it) or you can split them up by name.

Use[edit | edit source]

Now you can use the function document() everywhere in your code, for example in blade templates. This example only returns the same string it receives as an argument

Route::get('document/{text}', function($text) { 
    return document($text);

Now go to /document/foo in your browser (use php artisan serve or valet), which will return foo.