append select input to from in laravel(options have value from database)

Viewed 23

i have a from in laravel i want to append new row to the form there is a select input fild that has data from data base so i am not able to append the select input field can some one help me with that inventry.blade.php

  <input name="invetory[0][status]" value="0" type="hidden" />

            <td><input type="text" name="invetory[0][item]" class="form-control" required /></td>
            <td>
                <select class="form-control" name="invetory[0][type]">
                    @forelse($products as $product)

                    <option value=" {{$product->typeoffood}}">{{$product->typeoffood}}</option>
                    @empty
                    <option selected>Pick Type</option>
                    @endforelse

                </select>
            </td>
            <td><input type="text" name="invetory[0][tax]" class="form-control" required /></td>
            <td><input type="text" name="invetory[0][price]" class="form-control" required /></td>

            <td><input type="text" name="invetory[0][selling_price]" class="form-control" required /></td>
            <td><input type="text" name="invetory[0][total_price]" class="form-control" required /></td>

            </tr>
            <button type="button" name="add" id="add" class="btn btn-success">Add More</button>

app.blade.php


<script type="text/javascript">
    var i = 0;

    $("#add").click(function () {

        ++i;

    $("#dynamicTable").append('<tr><td><input type="text" name="invetory[' + i +
            '][item]" class="form-control" /></td><td><select class="form-control" name="invetory[0][type]">
                    @forelse($products as $product)

                    <option value=" {{$product->typeoffood}}">{{$product->typeoffood}}</option>
                    @empty
                    <option selected>Pick Type</option>
                    @endforelse

                </select></td><td><input type="text" name="invetory[' + i +
            '][tax]"  class="form-control" /></td><td><input type="text" name="invetory[' + i +
            '][price]" class="form-control" /></td><td><input type="text" name="invetory[' + i +
            '][selling_price]" class="form-control" /></td><td><input type="text" name="invetory[' + i +
            '][total_price]" class="form-control" /></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>'
        );
    });

    $(document).on('click', '.remove-tr', function () {
        $(this).parents('tr').remove();
    });

1 Answers

you can simply store $products variable in the javascript variable in inventory.blade.js and use it as a loop to append. for example let options;

$.each(products, function(key, value) {
        options+= `<option value=" ${val}">${val}</option>`;
    });

and you can append in current js code.

Related