
The giveConfig function in Laravel
3 Dec 2021 #development #laravel #php
Quite often, in Laravel, you need to inject configuration values into an object using the service container. To do so, there's a function called giveConfig
which can be used for that. An example:
$this->app->when(ReportAggregator::class) ->needs('$timezone') ->giveConfig('app.timezone');
This is essentially a shorthand for:
$this->app->when(ReportAggregator::class) ->needs('$timezone') ->give(fn () => config('app.timezone'));
It's one of those many small constructs in Laravel that make your code easier to read…