waqassiwag/laravel-variant-generator

A Laravel package to generate all possible product variants (Cartesian Product) from a set of attributes.

Maintainers

Package info

github.com/mohammadwaqas908/laravel-variant-generator

Homepage

Issues

pkg:composer/waqassiwag/laravel-variant-generator

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

v1.0.0 2026-06-13 04:56 UTC

This package is auto-updated.

Last update: 2026-06-13 05:09:34 UTC


README

A fast and fluent Laravel package to generate all possible product variants (Cartesian Product) from a set of attributes.

Run Tests Software License

Requirements

  • PHP 8.1+
  • Laravel 10.0+ / 11.0+ / 12.0+

Installation

composer require waqassiwag/laravel-variant-generator

Basic Usage

Pass an array of your product attributes to generate all possible combinations.

use Waqassiwag\VariantGenerator\Facades\VariantGenerator;

$attributes = [
    'Color' => ['Red', 'Blue'],
    'Size' => ['S', 'M'],
    'Material' => ['Cotton', 'Polyester']
];

$variants = VariantGenerator::make($attributes)->generate();

Output:

[
    ['Color' => 'Red', 'Size' => 'S', 'Material' => 'Cotton'],
    ['Color' => 'Red', 'Size' => 'S', 'Material' => 'Polyester'],
    ['Color' => 'Red', 'Size' => 'M', 'Material' => 'Cotton'],
    // ...
]

Advanced Features

1. Excluding Variants

Remove specific combinations that you don't want to generate.

VariantGenerator::make($attributes)
    ->exclude([
        ['Color' => 'Red', 'Size' => 'S']
    ])
    ->generate();

2. Custom Formatting

Modify the output exactly how you need it.

VariantGenerator::make($attributes)
    ->format(function ($variant) {
        return implode('-', $variant);
    })
    ->generate();

// Output: ['Red-S-Cotton', 'Red-S-Polyester', ...]

3. Generating SKUs

Automatically append a unique SKU to each variant.

VariantGenerator::make($attributes)
    ->sku(function ($variant) {
        return strtoupper(
            substr($variant['Color'], 0, 3) . substr($variant['Size'], 0, 1)
        );
    })
    ->generate();

Output:

[
    [
        'attributes' => ['Color' => 'Red', 'Size' => 'Small'],
        'sku' => 'REDS'
    ],
    // ...
]

4. Fluent API

You can chain all these methods together to build complex generation logic cleanly.

VariantGenerator::make($attributes)
    ->exclude([['Color' => 'Red', 'Size' => 'S']])
    ->sku(fn ($variant) => strtoupper(substr($variant['Color'],0,3).substr($variant['Size'],0,1)))
    ->format(fn ($variant, $processed) => $processed['sku'] . '-' . $variant['Material'])
    ->generate();

Tip: You can also call ->generateAsCollection() at the end if you prefer working with a Laravel Collection instead of an array.

Testing

composer test

Support & Security

Support

Credits

License

The MIT License (MIT). Please see License File for more information.