#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:

1$this->app->when(ReportAggregator::class)
2    ->needs('$timezone')
3    ->giveConfig('app.timezone');

This is essentially a shorthand for:

1$this->app->when(ReportAggregator::class)
2    ->needs('$timezone')
3    ->give(fn () => config('app.timezone'));

It's one of those many small constructs in Laravel that make your code easier to read…