Tag

Inject all bindings tagged with a given tag

Description: Injects all container bindings registered under a specific tag.

Namespace: Illuminate\Container\Attributes\Tag

Usage

use Illuminate\Container\Attributes\Tag;

class ReportAggregator
{
    public function __construct(
        #[Tag('reports')] private readonly iterable $reporters
    ) {}

    public function generate(): array
    {
        return collect($this->reporters)
            ->flatMap(fn ($reporter) => $reporter->data())
            ->all();
    }
}

In a service provider, register the tagged bindings:

$this->app->tag([SalesReport::class, TrafficReport::class], 'reports');