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 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题