Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ashisharyal's avatar

Inserting and updating data from dynamic form properly

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, i get an array like below {

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

}

Thanks in advance, This is my first application in Laravel. Im still learning, please dont mind if what im asking is stupid. This website has helped me alot

Ashish Aryal

0 likes
6 replies
ashisharyal's avatar

Finally made some progress and now i have the array after using Collection transpose() as described in https://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/ its a great read! now i have the arrays in following order which is one step forward.

[
 {
    "description": "desc1",
    "rate": "11",
    "quantity": "1"
    },
 {
    "description": "desc2",
    "rate": "22",
    "quantity": "2"
 },
 {
    "description": "desc3",
    "rate": "33",
    "quantity": "3"
 },
 {
    "description": "desc4",
    "rate": "44",
    "quantity": "4"
 }
]

Now I want to be able to update the arrays with invoice_id generated by $resultforinvoice = Invoice::create($datainvoice); so the arrays would look something like. Still trying to make this work. @Snapey Do you have any suggestions?

    [
     {
          "description": "desc1",
         "rate": "11",
          "quantity": "1",
        "invoice_id":"1"
        },
     {
        "description": "desc2",
          "rate": "22",
        "quantity": "2",
    "invoice_id":"1"
     },
     {
        "description": "desc3",
         "rate": "33",
         "quantity": "3",
    "invoice_id":"1"
     },
     {
        "description": "desc4",
        "rate": "44",
         "quantity": "4",
    "invoice_id":"1"
     }
    ]
Snapey's avatar
Snapey
Best Answer
Level 122

You don't need to alter the collection, just now use it to create your invoice lines.

foreach ($invoiceItems as $invoiceItem)
{
    InvoiceItem::create([
        'description' => $invoiceItem->description,
        'rate' => $invoiceItem->rate,
        'quantity' => $invoiceItem->quantity,
        'invoice_id' => $resultforinvoice->id,
        ]);
}
1 like
ashisharyal's avatar

@Snapey The answer you provided just fixed everything. Tomorrow I will be working on validation and update on these invoices. Hopefully it will go smoothly. I was about to pull my hair. I was just about to get friendly with laravel and i got stuck here. Thanks for helping me out. I appreciate it alot. I will be uploading whole thing with updating and validation when i finish it. Let me know if i need to worry about any thing for validations. give me some heads up :D

Thanks man, you are the MVP!!

Please or to participate in this conversation.