在数据库中保存多条记录 - 一对多关系

Saving Multiple Records In DB - One To Many Relationship

我正在使用 Laravel 5.7 & VueJs 2.5.* ...

我想保存我的 TicketInvoice 和它在数据库中的 TicketInvoiceItems 数据,当我提交我的发票表格时,TicketInvoice 数据存储在 DBTicketInvoiceItems 数据存储为空,无论我是否填充它,我也有 TicketInvoiceItems 的动态字段,因为 TicketInvoice hasMany() TicketInvoiceItems,所以无论我添加多少行, fill & submit, DB 中只有一行记录存储,而且我上面说了,所有 Items 字段都存储为 null.

这是我的 store 方法 TicketInvoiceController:

public function store(Request $request) {
  $ticketInvoiceItems = collect();

  $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']
  ]));

  $ticketInvoice = TicketInvoice::create([
    'vendor_id' => $request['vendor_id'],
    'ticket_invoice_no' => $request['ticket_invoice_no'],
    'ticket_invoice_date' => $request['ticket_invoice_date'],
    'ticket_invoice_fares_total' => $request['ticket_invoice_fares_total'],
    'ticket_invoice_grand_total' => $request['ticket_invoice_grand_total'],
  ]);

  $ticketInvoice - > ticketInvoiceItems() - > saveMany($ticketInvoiceItems);
}

这是我的 VueJs 代码,我不知道我在这里是否有任何错误,请检查它:

< script >
  export default {
    data() {
      return {
        ticketInvoices: {},
        vendors: null,
        form: new Form({
          id: "",
          vendor_id: "",
          ticket_invoice_no: "",
          ticket_invoice_date: "",
          ticket_invoice_fares_total: "",
          ticket_invoice_grand_total: "",

          ticketInvoiceItems: [{
            id: "",
            ticket_invoice_id: "",
            passenger_name: "",
            ticket_no: "",
            departure_date: "",
            fares: "",
            sub_total: ""
          }]
        })
      };
    },

    methods: {
      createTicketInvoice() {
        this.form
          .post("api/ticket-invoice")
          .then(() => {
            Fire.$emit("RefreshTable");
          })
          .catch(() => {
            swal("Failed!", "There was something wrong.", "warning");
          });
      },

      addItems() {
        this.form.ticketInvoiceItems.push({
          id: "",
          ticket_invoice_id: "",
          passenger_name: "",
          ticket_no: "",
          departure_date: "",
          fares: "",
          sub_total: ""
        });
      },
      removeItems(pos) {
        this.form.ticketInvoiceItems.splice(pos, 1);
      },
      <
      /script>

我得到了答案...

我没有正确访问请求中的 TicketInvoiceItems 数据。我目前在做:

$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']
]));

但是我从前端发回的请求数据将所有这些都包装在一个 ticketInvoiceItems.

数组中

所以我需要做类似的事情:

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']
  ]));
}

此外,我在 TicketInvoiceItems 上明确设置了 'ticket_invoice_id',因为这将由 saveMany() 方法填充。