Storage

Inject a storage disk instance

Description: Injects a specific storage disk instance by name.

Namespace: Illuminate\Container\Attributes\Storage

Added in: Laravel 11.20

Usage

use Illuminate\Container\Attributes\Storage;
use Illuminate\Contracts\Filesystem\Filesystem;

// Before (service provider):
$this->app->when(AvatarService::class)
    ->needs(Filesystem::class)
    ->give(fn () => Storage::disk('s3'));

use Illuminate\Container\Attributes\Storage;
use Illuminate\Contracts\Filesystem\Filesystem;

// After:
class AvatarService
{
    public function __construct(
        #[Storage('s3')] private readonly Filesystem $disk
    ) {}

    public function upload(string $path, mixed $contents): void
    {
        $this->disk->put($path, $contents);
    }
}

Enum values are also supported:

use App\Enums\StorageDisk;

#[Storage(StorageDisk::S3)] private readonly Filesystem $disk