waqassiwag/laravel-variant-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

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.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固