Home > OS >  Create a laravel post to create a variant product creator system
Create a laravel post to create a variant product creator system

Time:10-05

I am creating a CMS for an E-commerce and I am having problems developing a system to create variants of products.

I have the frontend developed and it seems like the image: enter image description here

As you can see, the system generates possible variants using the type of product in question. Then, the admin has to select which variants wants to create and click the submit button.

What I think is to make a form post, and get the request on the controller. With this approach, the request only contains the selected variants. But now, I have to create the variants and store them on DB, and with this information, I don't know how to make a dynamic iteration for each variant selected.
For example, if I press the submit button on the image, I get the following request:

{
    "_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"
}

To create a variant I need the N.º de producto and the Referencia values. For example, row1 would be a variant with code: "0001-0001" and value:"1"

This is the HTML code:

<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">Nde 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>

// EDIT

<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">Nde 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>

Controller code:

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

I hope someone can help me to find a solution to achieve it.

CodePudding user response:

Try changing you checkbox input to this

<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>

You will receive in your request

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

Where the value will be 0 or 1 depending if the checkbox is checked or not

--EDIT--

A better solution is to use the structure of an array.

<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>

This will result in a data structure like this

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

Controller Code

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
        }
    }
}
  • Related