douzhou7656 2016-12-10 11:01
浏览 182

Laravel 5如何将两个相关表插入(多个)到一个查询中

I have a table which is Order and Product

  • orders table: id, order_name
  • products table: id, orders_id, product_name

In my model: Order can have many products. Product belongs to order

Below is my view:

<input type="text" name="order_name"> 
<input type="text" name="product_name">

<input type="text" name="order_name"> 
<input type="text" name="product_name">

From the view above, I have two forms whereby the users can add two form with two input.

Now my problem is how do I write the logic in the controller?

I just know the way to write insert one order with multiple product. But I don't know how to insert multiple orders with its related product.

  • 写回答

2条回答 默认 最新

  • dongxuan8227 2016-12-10 11:07
    关注

    You should use a pivot table, as it's a many to many relationship between the two tables.

    Since a product belongs to not just one order but many and an order can have many products, pivot table is the way to go.

    See here if you don't know what a pivot table is : http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

    They explain it in an example very similar to yours.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊