Search code examples
laravelhelper

Laravel loading helper not automatically


I tried to load my helper without composer autoload

For controllers I use:

use \App\Helper;

Works good, But for blade's view how can I load it?


Solution

  • You can do it 2 ways

    Solution 1: make aliases

    In config\app.php change aliases to

    'aliases' => [
        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        ...................
        'Helper' => App\Helper::class,
    ]
    

    in blade use

    @php
        $result = Helper::staticFunction();
        // or
        $helper = app(Helper::class);
        $helper->functionName(); 
    @endphp
    

    Solution 2:

    @php
        $result = \App\Helper::staticFunction();
        // ot
        $helper = app(\App\Helper::class);
        $helper->functionName();
    @endphp