简体   繁体   中英

Passing data using array from View (Blade) to Controller in laravel

I have a blade form which has a repeater input box and a select box, the repeater works fine but how do i pass the data from those fields to back-end controller in laravel?

add.balde.php

<!--begin::Add-Invoice-Form-->
<form class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" action="{{route('store_invoice')}}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}
    <div class="m-portlet__body">
        <div class="form-group m-form__group row">

            <div class="col-lg-4">
                <label>
                    Customer Name:
                </label>
                <select class="form-control m-select2" id="m_select2_1" name="customerId">
                    @foreach($customer_list as $customer)
                        <option value=" {{ $customer->id }} ">
                        {{ $customer->fName }} {{ $customer->mName }} {{ $customer->lName }}
                        </option>
                    @endforeach  
                </select>
            </div>
            <div class="col-lg-4">
                <label>
                    Invoice Type:
                </label>
                <div class="m-radio-inline">
                    <label class="m-radio m-radio--solid">
                        <input type="radio" name="invoiceType" checked value="billable">
                        Billable
                        <span></span>
                    </label>
                    <label class="m-radio m-radio--solid">
                        <input type="radio" name="invoiceType" value="nonbillable">
                        Non Billable
                        <span></span>
                    </label>
                </div>
            </div>
        </div>

     <div id="m_repeater_1">

        <div class="form-group row" id="m_repeater_1">
            <div data-repeater-list="" class="col-lg-12">
                <div data-repeater-item class="form-group m-form__group row align-items-center">
                    <div class="col-lg-4">
                <label>
                    Summary Number:
                </label>
                <select class="form-control m-select2" id="m_select2_2" name="certificateId[]">
                    @foreach($certificate_list as $certificate)
                        <option value=" {{ $certificate->id }} ">
                        {{ $certificate->summary_no }} ( {{ $certificate->certificateType() }} )
                        </option>
                    @endforeach  
                </select>
            </div>
                    <div class="col-lg-3">
                                <label>
                                    Rate:
                                </label>
                                <input type="number" class="form-control m-input" name="rate" placeholder="Enter rate">
                    </div>
                    <div class="col-lg-3">
                        <br/>
                        <div data-repeater-delete="" class="btn btn btn-danger m-btn m-btn--icon">
                            <span>
                                <i class="la la-trash-o"></i>
                                <span>
                                    Remove
                                </span>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="m-form__group form-group row">
            <div class="col-lg-4">
                <div data-repeater-create="" class="btn btn btn-warning m-btn m-btn--icon">
                    <span>
                        <i class="la la-plus"></i>
                        <span>
                            Add
                        </span>
                    </span>
                </div>
            </div>
        </div></div>


    </div>

    <div class="m-portlet__foot m-portlet__no-border m-portlet__foot--fit">
        <div class="m-form__actions m-form__actions--solid">
            <div class="row">
                <div class="col-lg-4"></div>
                <div class="col-lg-8">
                    <button type="submit" class="btn btn-primary">
                        Submit
                    </button>
                    <button type="reset" class="btn btn-secondary">
                        Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>
<!--end::Add-Invoice-Form-->

How do I send the values in array format when multiple select box have been filed using repeater? This images shows the UI for the form

Thank you

you can try something like this

 <input type="text" value="val" name="somename[]">
 <select name="someSelectName[]">
    <option value="value">select 1
    </option> 
 </select>

on server side you will receive selected values as array with given name

Change your

<div data-repeater-list="" class="col-lg-12">

To

<div data-repeater-list="arrayName" class="col-lg-12">

and It should work.

This makes the all the input names to have an array format of arrayName[number][input] and the number increments for each input and finally in the request sent you will have all the inputs in the arrayName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM