mercredi 17 janvier 2018

Laravel How to compare an input array collection with the array collection selected from the database

I really need assistance with an issues i have been having for a week. i do have an invoice form enter image description here of which i could add multiple items in an array to store in the database

<?php
   $arrayNumber = 0;
      for($x = 1; $x < 4; $x++) { ?>
     <tr id="row<?php echo $x; ?>" class="orderItem  <?php echo $arrayNumber; ?>">
        <td>
                                                <div class="form-group">
                                                    <select class="js-example-basic-single form-control" name="productName[]" id="productName<?php echo $x; ?>" onchange="getProductData(<?php echo $x; ?>)" required>
                                                        <option value="">~~SELECT~~</option>
                                                        @foreach($stocks as $stock)
                                                            <option value=""></option>
                                                        @endforeach
                                                    </select>
                                                </div>
                                            </td>
                                            <td>
                                                <div class="form-group row"  style="padding-left:10px;padding-right:10px">
                                                    <input type="number" name="rate[]" id="rate<?php echo $x; ?>" autocomplete="off" class="form-control" required/>                             
                                                    <!-- <input type="hidden" name="rateValue[]" id="rateValue<?php echo $x; ?>" autocomplete="off" class="form-control"  required/>   -->
                                                </div>
                                            </td>

                                            <td>
                                                <div class="form-group row"  style="padding-left:10px;padding-right:10px">
                                                    <input type="number" name="quantity[]" id="quantity<?php echo $x; ?>" onkeyup="getTotal(<?php echo $x ?>)" autocomplete="off" class="form-control" min="1" required/>

                                                </div>
                                            </td>
                                            <td>
                                                <div class="form-group row"  style="padding-left:10px;padding-right:10px">
                                                    <input type="text" name="total[]" id="total<?php echo $x; ?>" autocomplete="off" class="form-control" disabled="true" />                                
                                                    <input type="hidden" name="totalValue[]" id="totalValue<?php echo $x; ?>" autocomplete="off" class="form-control" />    
                                                </div>
                                            </td>
                                            <th>
                                                <button class="btn btn-default removeProductRowBtn" type="button" id="removeProductRowBtn" onclick="removeProductRow(<?php echo $x; ?>)">
                                                    <i class="icofont icofont-minus"></i>
                                                </button>
                                            </th>
                                        </tr>
                                    <?php
                                    $arrayNumber++;
                                    } // /for
                                    ?>

storing in the db as

$data = $request->all();

    $productName = count($data['productName']);
    for($i=0; $i < $productName; $i++){

        $stt = new StockTransferTransaction;

        $stt->stock_id = $data['productName'][$i];
        $stt->rate = $data['rate'][$i];


        $stt->quantity = $data['quantity'][$i];
        $stt->valuation = $data['totalValue'][$i];
        $stt->stock_transfer_id = $id;
        $stt->save();

        // go to stock

        $stock = Stock::where('id', $productName)->get();

        dd($stock);

        // save all
        $quantityApproval->save();
        $stock->save();
    }

Which is successful

Here is the issue i would like make some checks from another table like makbe if the quantity request is more that what is in that table then returns an error before saving to the database before saving to the database

with my effort i used a

$data = $request->all();

    $productName = count($data['productName']);
    for($i=0; $i < $productName; $i++){

    $stock = Stock::select('quantity')->whereIn('id', $data['productName'])->get();
     // $stock = Stock::find($data['productName']);
     dd($stock, $request->all());
    }

enter image description here

and dont know how to compare the request array and the collection result for at least the quantity to return a response. Please i need help badly. Anybody here, Somebody, anybody. Thanks alot



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FKNVJe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire