dpnhp20440 2016-07-31 08:21
浏览 286
已采纳

Laravel 5.2从动态表单插入和更新数据

So far, I have found answers in here and never needed to ask anything. But this issue is really bugging me. Here is the situation. I have a dynamic invoice form. Posts from the form will be saved to two different tables

  1. invoices(where details for invoice is stored like member_id, discount, duedate, remarks and the timestamps)
  2. invoiceitems(invoice_id, itemdescription, quantity, rate, timestamps)

To achieve this, here is my method to store invoice

public function store(InvoiceRequest $request)
{

//get necessary details for invoice table
$datainvoice = array(
'member_id' => $request->input('member_id'),
'duedate' => $request->input('duedate'),
'discount' => $request->input('discount'),
'remarks' => $request->input('remarks'),
//'gtotal' => $request->input ('gtotal'),
);

//save result of invoice
$resultforinvoice = Invoice::create($datainvoice);


// ready all data of invoice items
$datainvoiceitem = array(
'description' => $request->input('description'),
'rate' => $request->input('rate'),
'quantity' => $request->input('quantity'),
'invoice_id' => $resultforinvoice->id,
);


 // insert invoice along with invoice_id
Invoiceitem::create($datainvoiceitem);
return redirect('members');

}

Additional Information:

Relationship between members and invoices:: member-has-many-invoices, invoice-belongs-to-single-member

Relationship between invoice and invoiceitems:: invoice-has-many-items, an-item-belongs-to-one-invoice

partial of the dynamic form:

<td>  1   </td>
<td><textarea type="text" name='description[0][description]'   class="form-control"/></textarea> </td>
<td><input type="text" name='quantity[0][quantity]'  class="form-control"/></td>
<td> <input type="text" name='rate[0][rate]'  class="form-control"/>   </td>

Errors im receiving right now Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in C:\xampp\htdocs\study\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 719 and defined

Update

when i try to return the inputs for invoiceitems to check whats going on, i get an array like below

{
"description": [
{
    "description": "desc1"
},
{
    "description": "desc2"
}
],
"rate": [
{
    "rate": "11"
},
{
    "rate": "22"
}
],
"quantity": [
{
    "quantity": "1"
},
{
    "quantity": "2"
}
],
"invoice_id": 41

}

Guys, please tell let me know where im missing. I have tried asking this in laracast as well, its been couple of hours nothing at the moment, hopefully someone will point me to right direction. Here is the laracast link.

  • 写回答

2条回答 默认 最新

  • dongzz4545 2016-08-27 11:17
    关注

    I ended up updating majority of the code and now it looks like this

     $items = [];
        foreach ($request->items as $field => $values) {
            foreach ($values as $index => $value) {
                $items[$index][$field] = $value;
            }
        }
    
    
        Invoice::create($invoice)
            ->invoiceitem()->createMany($items);
    

    and it just worked. Let me know anyone else needs help with this. I would be happy to explain

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doubao6681 2016-08-01 19:41
    关注

    May I suggest

    public function store(InvoiceRequest $request)
    {
        $invoice_fields = ['member_id', 'duedate', 'discount', 'remarks', 'gtotal'];
    
        $items = [];
    
        foreach ($request->items as $field => $values) {
            foreach ($values as $index => $value) {
                $items[$index][$field] = $value;
            }
        }        
    
        Invoice::create($request->only($invoice_fields))
            ->items()->createMany($items);
    
        return redirect('members');    
    }
    

    (If you set up the relation properly, you don't need to manually assign the invoice_id to each item, just have the Invoice create the Item).


    You may abstract the foreachs into a separate method, a Collection macro may be a good idea as the tutorial suggests (although I implement it differently; my suggestion doesn't need index numbers in the original HTML). The HTML for your form should look like

    <input type="text" name="items[description][]">
    <input type="text" name="items[quantity][]">
    <input type="text" name="items[rate][]">
    
    <input type="text" name="items[description][]">   <!-- as many of --> 
    <input type="text" name="items[quantity][]">      <!-- these      -->
    <input type="text" name="items[rate][]">          <!-- as needed  -->
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信
  • ¥15 使用QT实现can通信
  • ¥15 关于sp验证的一些东西,求告知如何解决,