创建laravelpost创建变体产品创建系统

Create a laravel post to create a variant product creator system

我正在为电子商务创建 CMS,但在开发用于创建产品变体的系统时遇到问题。

我已经开发了前端,它看起来像图像:

如您所见,系统使用相关产品类型生成了可能的变体。然后,管理员必须 select 要创建哪些变体并单击提交按钮。

我想的是做一个表单post,在controller上获取request。使用这种方法,请求仅包含 selected 变体。但是现在,我必须创建变体并将它们存储在数据库中,有了这些信息,我不知道如何为每个变体进行动态迭代 selected.
例如,如果我按下图像上的提交按钮,我会收到以下请求:

{
    "_token":"OQElUkt7zMaxr1JkZU6fw1r9gcaBuLa6gXHKSsex",
    "row-check-0":"0001-0001",
    "row-check-1":"0001-0002",
    "row-check-2":"0001-0003",
    "row-check-3":"0001-0004",
    "row-check-4":"0001-0005"
}

要创建变体,我需要 N.º de productoReferencia 值。例如,第 1 行将是代码为“0001-0001”且值为:“1”

的变体

这是HTML代码:

<div class="inside">
    {!! Form::open(['url' => '/admin/products/'.$product->id.'/variants/new']) !!}
    <table class="table table-striped">
        <thead>
            <tr>
                <th scope="col" style="text-align: center; width:5%">
                    <input type="checkbox" id="masterCheck" value="checkUncheckAll">
                </th>
                <th scope="col">N.º de producto</th>
                <th scope="col">{{$product->type_product->name}}</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $count = 0;
                $last_code = 0;
            ?>
            @foreach($no_used_values as $no_used_value)
                <?php
                    $last_code = getNextCode($last_code);
                ?>
                <tr>
                    <td class="table-text" style="padding: 0px; text-align: center; vertical-align: middle;">
                        <input type="checkbox" id="row{{$count}}" name="row-check-{{$count}}" value="{{$product->code.'-'.$last_code}}">
                    </td>
                    <td class="table-text">{{$product->code.'-'.$last_code}}</td>
                    <td class="table-text">{{$no_used_value->value}}</td>
                </tr>
                <?php
                    $count++;
                ?>
            @endforeach
        </tbody>
    </table>
    <div id="dialog-button-bar-variants" class="dialog-button-bar">
        <button type="button" class="btn btn-primary" onclick="goToValues()">&lt;&lt; Atrás</button>
        {!! Form::submit('Crear Variantes', ['class' => 'btn btn-primary']) !!}
        <div>   
            {!! Form::close() !!}           
        </div>
    </div>
</div>

// 编辑

<div class="inside">
    {!! Form::open(['url' => '/admin/products/'.$product->id.'/variants/new']) !!}
    <table class="table table-striped">
        <thead>
            <tr>
                <th scope="col" style="text-align: center; width:5%">
                    <input type="checkbox" id="masterCheck" value="checkUncheckAll">
                </th>
                <th scope="col">N.º de producto</th>
                <th scope="col">{{$product->type_product->name}}</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $count = 0;
                $last_code = 0;
            ?>
            @foreach($no_used_values as $no_used_value)
                <?php
                    $last_code = getNextCode($last_code);
                ?>
                <tr>
                    <td class="table-text" style="padding: 0px; text-align: center; vertical-align: middle;">
                        <input type="checkbox" id="row{{$count}}" key="row-check" name="last_codes[{{$product->code.'-'.$last_code}}]" value="{{$no_used_value->value}}" >
                    </td>
                    <td class="table-text">{{$product->code.'-'.$last_code}}</td>
                    <td class="table-text">{{$no_used_value->value}}</td>
                </tr>
                <?php
                    $count++;
                ?>
            @endforeach
        </tbody>
    </table>
    <div id="dialog-button-bar-variants" class="dialog-button-bar">
        <button type="button" class="btn btn-primary" onclick="goToValues()">&lt;&lt; Atrás</button>
        {!! Form::submit('Crear Variantes', ['class' => 'btn btn-primary']) !!}
        <div>   
            {!! Form::close() !!}           
        </div>
    </div>
</div>

控制器代码:

public function postProductsVariantCreator($id, Request $request){
    //$product = Product::findOrFail($id);
    foreach ($request->last_codes as $key => $value) {
        //code to store the variants
    }
}

我希望有人能帮我找到一个解决方案来实现它。

尝试将您的复选框输入更改为此

<td class="table-text" style="padding: 0px; text-align: center; vertical-align: middle;">
    <input type="hidden" name="{{$product->code.'-'.$last_code}}" value="0">
    <input type="checkbox" id="row{{$count}}" name="{{$product->code.'-'.$last_code}}" value="1">
</td>

您将在请求中收到

{
    "_token":"OQElUkt7zMaxr1JkZU6fw1r9gcaBuLa6gXHKSsex",
    "0001-0001":"1",
    "0001-0002":"0",
    "0001-0003":"1",
    "0001-0004":"1",
    "0001-0005":"0"
}

如果复选框被选中,值将是 0 或 1

--编辑--

更好的解决方案是使用数组结构。

<td class="table-text" style="padding: 0px; text-align: center; vertical-align: middle;">
    <input type="hidden" name="{{$product->code.'-'.$last_code}}" value="0">
    <input type="checkbox" id="row{{$count}}" name="last_codes[{{$product->code}}][{{$last_code}}]" value="1">
</td>

这将导致这样的数据结构

{
    "_token":"OQElUkt7zMaxr1JkZU6fw1r9gcaBuLa6gXHKSsex",
    "last_codes":[
        "0001":[
            "0001":"1",
            "0002":"0",
            "0003":"1",
            "0004":"1",
            "0005":"0"
        ]
    ]
}

控制器代码

public function postProductsVariantCreator($id, Request $request){
    //$product = Product::findOrFail($id);
    foreach ($request->last_codes as $productCode => $lastCodes) {

        //here you can fetch the product using the $productCode

        foreach($lastCodes as $lastCode => $enabled) {
            //here you have access to $lastCode && $enabled where $enabled = 1 or 0
        }
    }
}