Request data from form with multiple inputs using

2019-08-09 07:47发布

So I've created a form that that will submit to two separate tables. It's for invoice building. Each invoice has a variable amount of rows, so I've created two tables, one for the invoice and one for each row. The rows are tied to the invoice table via a foreign key.

With pure PHP it's simply a matter of naming the inputs "name[]" and then looping through them. But in Laravel, it's not seeming to work. Basically, I have:

$invoice = new Invoice;
$invoice->fill($data);
$invoice->save();
for ($i = 0; $i < count($request->item); $i++){
    $row = new InvoiceRow;
    $row->fill(array('item' => $request->item[$i], 'description' => $request->description[$i], 'cost' => $request->cost[$i], 'quantity' => $request->quantity[$i], 'total' => $request->itemTotal[$i]));
    $invoice->InvoiceRows()->save($row);
}

It will save the first row, but not the other rows.

Edit: Here is some of the markup, without the classes for bootstrap and validation. The row is being cloned with jQuery for each neccessary line for the document.

<tr class="item-row">
                        <td> class="form-control form-inline item-name" name="item[]">
                        </td>
                        <td>
                            <input type="text" name="description[]">
                        </td>
                        <td>
                            <input type="number"  name="cost[]">
                        </td>
                        <td>

                                <input name="quantity[]">

                        </td>
                        <td>
                            <input type="number" name="itemTotal[]">
                        </td>
                    </tr>

标签: php laravel-5
1条回答
看我几分像从前
2楼-- · 2019-08-09 08:23

Iterate over that object e.g

$name = $request->name; //here name is the name of your form's name[] field

foreach($name as $names)
{
//do something
}
查看更多
登录 后发表回答