找到你要的答案

Q:add field to foregin key table and query with eloquent

Q:添加字段到外国关键表和查询功能

I've 2 Models, let's say: Cart and Product.

Cart has many Product. Product belongs to Cart

3 tables cart, product, cart_product (id, cart_id, product_id)

I insert another field to cart_product, ex: (id, cart_id, product_id, quantity).

When I attach a Product to a Cart how can I query the relation to insert a quantity?

我有2种型号,比如说:购物车和产品。

购物车有很多产品。产品属于购物车

表3车,产品,cart_product(ID,cart_id,product_id)

我cart_product插入另一个领域,例如:(身份证,cart_id,product_id,数量)。

当我将产品连接到购物车时,我如何查询插入数量的关系?

answer1: 回答1:

self answer:

$cart->products()->attach($p, array('blablabla', 'blablabla'));

自我回答:

$cart->products()->attach($p, array('blablabla', 'blablabla'));
laravel  key  eloquent