Answer the question
In order to leave comments, you need to log in
Calling a helper in a Laravel template?
I am learning Laravel. There was such a question.
Is it possible to call a service provider from a Laravel template?
I found the following fragment in the documentation:
"You can insert into the template any class that is defined in the service container of the framework. For this, the @inject directive is used, the first argument of which is the name of the variable in which the class instance will be placed, and the second is the name of the injected class or interface"
And the example itself:
@inject('metrics', 'App\Services\MetricsService')
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
in the inject I specified the path to my service provider App\Providers\RatesProvider
But I get an error
Unresolvable dependency resolving [Parameter #0 [ $app ]] in class Illuminate\Support\ServiceProvider (View: D:\OpenServer\domains\myblog.ru\resources\views\admin\edit-post.blade.php
) simple, just returns a specific value when called from a controller it works.
Here is the register function in the service provider
public function register(){
$this->app->bind('App\Helpers\RatesContract', function () {
return new RatesController($this->app);
});
}
<?php
namespace App\Helpers;
Interface RatesContract
{
public function getRate();
}
<?php
namespace App\Helpers;
use App\Helpers\RatesContract;
class RatesController implements RatesContract
{
public function getRate(){
return "123123";
}
}
Answer the question
In order to leave comments, you need to log in
It is not the provider that needs to be injected, but the RatesContract interface itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question