Javascript 基于ID插入多个项目-Laravel
我有两个表Javascript 基于ID插入多个项目-Laravel,javascript,php,laravel,Javascript,Php,Laravel,我有两个表products和shippings,以及透视表product\u shipping,其中包含product\u id和shipping\u no字段。在透视表中,可以有一个装运编号,例如#4562,其中包含2个或更多产品。这是我想要实现的 这就是我的代码的工作原理。当我选择产品hammer时,我能够检索hammer的id。当我选择另一个产品螺丝刀的id时,它将替换锤子的id,而不是同时存储两者 插入数据后,我希望在我的透视表中找到类似的内容 product_id name
products
和shippings
,以及透视表product\u shipping
,其中包含product\u id和shipping\u no
字段。在透视表中,可以有一个装运编号,例如#4562,其中包含2个或更多产品。这是我想要实现的
这就是我的代码的工作原理。当我选择产品hammer
时,我能够检索hammer
的id。当我选择另一个产品螺丝刀
的id时,它将替换锤子
的id,而不是同时存储两者
插入数据后,我希望在我的透视表中找到类似的内容
product_id name
1 hammer
2 screwdriver
shipment_no courrier
4562 MaerskLine
产品表
product_id name
1 hammer
2 screwdriver
shipment_no courrier
4562 MaerskLine
发货表
product_id name
1 hammer
2 screwdriver
shipment_no courrier
4562 MaerskLine
产品装运-枢轴
shipment_no product_no
4562 1
4562 2
这是我选择我的产品复选框的位置
HTML
<input onclick="return productSelected(this)" type="checkbox"
id="{!! $product->id !!}" name="{!! $product->name !!}" />
装运模式
public function products()
{
return $this->belongsToMany('App\Product')
->withTimestamps();
}
public function shipments()
{
return $this->belongsToMany('App\Shipment')
->withTimestamps();
}
产品型号
public function products()
{
return $this->belongsToMany('App\Product')
->withTimestamps();
}
public function shipments()
{
return $this->belongsToMany('App\Shipment')
->withTimestamps();
}
如何获取所选产品的所有id,并使其与我的
透视表中的产品相同?
感谢您的帮助首先更改输入名称如下:
function productSelected(product)
{
$(".panel").append(
'<input type="text" value='+product.id+' " id="product'+product.id+'"
name="products[]" />'
}
当然,在您的
发货
型号中,您应该与产品
有manytomy
关系。您在型号中有关系吗not@Maraboc是的,我和模特有关系。这是一个多对多的关系。你能添加两个模型的代码吗?@Maraboc我已经添加了模型。请检查我的更新,这太简单了。非常感谢你