fbc
4 days ago

Using Service Providers

Posted 4 days ago by fbc

I need to share report creating functionality among my controllers. So I ran:

php artisan make:provider ReportServiceProvider

then I added:

use App\Providers\ReportServiceProvider;

to all the controllers I expected to use it in.

Then I tried various methods to call the functions but failed.

$forecast = ReportServiceProvider::class->get_dark_sky_weather($propdash->lat,$propdash->lng);
$forecast = app(ReportServiceProvider::class)->get_dark_sky_weather($propdash->lat,$propdash->lng);
$forecast = App::make(ReportServiceProvider::class)->get_dark_sky_weather($propdash->lat,$propdash->lng);

I used to use this:

$forecast = $this->get_dark_sky_weather($propdash->lat,$propdash->lng);

when the function was in the same controller. But since I've moved it to the Service Controller I can nolonger access it.

I've tried reading through the Laravel Documentation for Service Providers, but can't seem to make heads or tails of it. Any reference to a working example would be appreciated.

Please sign in or create an account to participate in this conversation.