动态多个输入未保存在数据库中

时间:2020-02-26 07:25:15

标签: laravel

我需要有关动态多重保存的帮助。我尝试了此代码,但无法正常工作,也许您知道如何正确纠正此代码才能使其正常工作?已经完成了许多测试,但无能为力

//控制器代码

public function store(Request $request)
    {
        $this->validate($request, [
            'date' => 'required',
            'due_date' => 'required',
        ]);

        $invoice = new Invoice;
        $invoice->company_id = Auth()->user()->id;
        $invoice->date = $request->input('date');
        $invoice->due_date = $request->input('due_date');
        $invoice->number = $request->input('number');
        $invoice->terms = $request->input('terms');
        $invoice->user_id = Auth()->user()->id;
        $invoice->sub = Auth()->user()->id;
        $invoice->vat = Auth()->user()->id;
        $invoice->grand = Auth()->user()->id;

        if ($invoice->save()) {
            for ($i = 0; $i < 2; ++$i) {
                $invoice_list = new Invoice_list;
                $invoice_list->description = $input['description'][$i];
                $invoice_list->qty = $input['qty'][$i];
                $invoice_list->price = $input['price'][$i];
                $invoice_list->amount = $input['amount'][$i];
                $invoice_list->invoice_id = Auth()->user()->id;
                $invoice_list->save();
            }
        }

        return redirect('/invoices');
    }

刀片代码

{!! Form::open(['action' => 'InvoiceController@store', 'method' => 'POST']) !!}
{{csrf_field()}}
<div class="card bg-secondary shadow">
    <div class="card-header bg-white border-0">
        <div class="row align-items-center">
            <div class="col-8">
                <h3 class="mb-0">New invoice </h3>
            </div>
            <div class="col-4 text-right">
                {{ Form::submit('Create', ['class' => 'btn btn-sm btn-primary']) }}
            </div>
        </div>
    </div>
    <div class="card-body">
        <div class="pl-lg-4">
            <div class="row">
                <div class="col-lg-3">
                    <div class="form-group">
                        <label class="form-control-label">Company</label>
                        <select class="form-control form-control-alternative" name="company_id" required>
                            <option value="1">UAB Vilniaus pakuote</option>
                            <option value="2">UAB Kauno tiltai</option>
                        </select>
                    </div>
                </div>
                <div class="col-lg-3">
                    <div class="form-group">
                        <label class="form-control-label">Date</label>
                        <input class="form-control form-control-alternative datepicker-here" name="date"
                               data-language='en' placeholder="Select date" type="text">
                    </div>
                </div>
                <div class="col-lg-3">
                    <div class="form-group">
                        <label class="form-control-label">Due date</label>
                        <input class="form-control form-control-alternative datepicker-here" name="due_date"
                               data-language='en' placeholder="Select date" type="text">
                    </div>
                </div>
                <div class="col-lg-3">
                    <div class="form-group">
                        <label class="form-control-label">Number</label>
                        <input class="form-control form-control-alternative" name="number" placeholder="Auto generated"
                               type="text">
                    </div>
                </div>
            </div>
            <hr class="my-4"/>
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-bordered" id="dynamic_field">
                        <thead>
                        <tr>
                            <th scope="col">Description</th>
                            <th scope="col">Qty</th>
                            <th scope="col">Price</th>
                            <th scope="col">Amount</th>
                            <th scope="col">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input class="form-control form-control-alternative" type="text" name="description[]"
                                       placeholder="Description"></td>
                            <td><input class="form-control form-control-alternative qty" type="text" name="qty[]"
                                       placeholder="Qty"></td>
                            <td><input class="form-control form-control-alternative price" type="text" name="price[]"
                                       placeholder="Price"></td>
                            <td><input class="form-control form-control-alternative amount" type="text" name="amount[]"
                                       placeholder="Amount"></td>
                            <td>
                                <center>
                                    <button type="button" name="add" id="add" class="btn btn-sm btn-success"><i
                                            class="fas fa-plus"></i></button>
                                </center>
                            </td>
                        </tr>
                        </tbody>
                        <tbody>
                        <tr>
                            <td scope="col">Sub Total:</td>
                            <td class="sub">0.00</td>
                        </tr>
                        <tr>
                            <td scope="col">VAT(21%):</td>
                            <td class="vat">0.00</td>
                        </tr>
                        <tr>
                            <td scope="col">Grand Total:</td>
                            <td class="grand">0.00</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <hr class="my-4"/>
            <div class="row">
                <div class="col-lg-12">
                    <div class="form-group">
                        <label class="form-control-label">Terms and Conditions</label>
                        <textarea class="form-control form-control-alternative" name="terms" rows="5"
                                  placeholder="Terms and Conditinios" required></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{!! Form::close() !!}

0 个答案:

没有答案