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…
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.