Laravel Insert A Multiple Inputs

- 1 answer

i have issue in laravel 5.1 with insert a multiple inputs intro DB.

i build a query code for multiple inputs and after that i send them to controller but i have there a issues to insert it to DB

how i can insert the inputs to db if the results its like this:


the code:


                 <form class="js-validation-material form-horizontal push-10-t" action="{!! url() !!}/addreg" method="post">
                                {!! csrf_field() !!}
                                <div class="form-group">
                                    <div id="buildyourform"></div>
                                <div class="form-group">
                                    <div class="col-xs-12">
                                        <button class="btn btn-sm btn-primary" type="submit">Submit</button>
                $(document).ready(function() {
                $("#add").click(function() {
                    var intId = $("#buildyourform div.form").length + 1;
                    var fieldWrapper = $("<div class=\"form col-sm-6 col-lg-6\" id=\"field" + intId + "\"><div class=\"form-material\">");
                    var client_id = $("<input type=\"hidden\" name=\"client_id[]\" value=\"{!! $task->client_id !!}\" class=\"form-control\" />");
                    var langname = $("<select type=\"text\" name=\"short[]\" class=\"form-control\">{!! $data['langs'] !!}</select>");
                    var url = $("<input type=\"text\" name=\"url[]\" class=\"form-control\" placeholder=\"Insert a url..\" />");
                    var label = ("<label for=\"date\"><h3 class=\"block-title\">Lang" + intId + "</h3></label>");
                    var removeButton = $("<button class=\"btn btn-danger  btn-xs push-5-r push-10\" type=\"button\"><i class=\"fa fa-times\"></i></button>");

                    $("#field" + intId + " .form-material").append(client_id,langname,url,removeButton,label);


the results need to be in the DB like:

        ['client_id' => $request->client_id],
        ['short' => $request->short],
        ['url' => $request->url],

for each input.



The problem is you are sending multiples inputs with the same name, so it only get the last one.

Everytime you do and "add" try to wrap them in a div with a class (example: "foo"). So you will get some "foo" divs.

Create a hidden input called for example "items"

{{ Form::hidden('items') }}

Next you have to overwrite the click function of the submit button:

    a = {};
    $.each($('.foo'), function(k,v){
        a[k] = $(v).find(':input').serializeArray()

And in the controller:

$items = json_decode(Input::get('items'), true);    

Then you have an array with all of your inputs.