Beusebiu
Beusebiu

Reputation: 1523

Access class from helper in controller Laravel

I created an helper and I try to use it in one of my controllers,but I got an error, and I am not sure why.

//StringHelper.php
namespace App\Helpers;

class StringHelper
{
    public function example($str1){
        //CODE
    }
}


//config/app.php
'aliases' => [
    'StringHelper' => App\Helpers\StringHelper::class,
]


//In controller 
use StringHelper;

$percentage = StringHelper::example($title);

Non-static method App\Helpers\StringHelper::example() should not be called statically

Upvotes: 1

Views: 782

Answers (1)

TsaiKoga
TsaiKoga

Reputation: 13394

Because the method example($str1) is not static, you need to call it by instance.

I think you are calling other instance's methods in example, so the simple way is call the method by instance.

$helper = new StringHelper();
$percentage = $helper->example($title);

Or you need to defined all those methods to static.

Upvotes: 1

Related Questions