[Vuejs]-Saving Multiple Records In DB – One To Many Relationship

0👍

I got my answer…

I’m not accessing the TicketInvoiceItems data correctly in the request. I’m currently doing:

$ticketInvoiceItems - > push(new TicketInvoiceItems([
  'ticket_invoice_id' => $request['ticket_invoice_id'],
  'passenger_name' => $request['passenger_name'],
  'ticket_no' => $request['ticket_no'],
  'departure_date' => $request['departure_date'],
  'fares' => $request['fares'],
  'sub_total' => $request['sub_total']
]));

But my request data I’m sending back from the front end has all of this wrapped up in an array of ticketInvoiceItems.

So i need to do something like:

foreach($request['ticketInvoiceItems'] as $invoiceItem) {
  $ticketInvoiceItems - > push(new TicketInvoiceItems([
    'ticket_invoice_id' => $invoiceItem['ticket_invoice_id'],
    'passenger_name' => $invoiceItem['passenger_name'],
    'ticket_no' => $invoiceItem['ticket_no'],
    'departure_date' => $invoiceItem['departure_date'],
    'fares' => $invoiceItem['fares'],
    'sub_total' => $invoiceItem['sub_total']
  ]));
}

Also I’m explicitly setting 'ticket_invoice_id' on the TicketInvoiceItems as this will get populated by the saveMany() method.

Leave a comment