waqassiwag/laravel-variant-generator
Composer 安装命令:
composer require waqassiwag/laravel-variant-generator
包简介
A Laravel package to generate all possible product variants (Cartesian Product) from a set of attributes.
README 文档
README
A fast and fluent Laravel package to generate all possible product variants (Cartesian Product) from a set of attributes.
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
-
Issues: Open an issue in GitHub
-
Security: If you discover any issues, please email
m.waqas7375@gmail.com.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-13