Helper methods in Laravel

By | 05/12/2019

Rails gives you a great helper framework for each controller, allowing you to keep the logic out of the view, wanting to replicate but looks like Laravel takes a different approach.

First create a helper file in the root of the app

$ touch helper.php

Now add the following

namespace App\Helpers;

class Helper
{
    public static function helperName()
    {
        return "whatever is returned";
    }
}

In the config/app.php file fine the ‘aliases’ section and add the following

'Helper' => App\Helpers\Helper::class

In the terminal run

$ composer dump-autoload 

Now use the helper method in the view anywhere you like by calling

{!! Helper::helperName() !!}