为数据透视表添加模型 ItemSupplier whth extra column id ,扩展 Pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class ItemSupplier extends Pivot
{
public function purchaseOrder()
{
return $this->belongsToMany(PURCHASE_ORDERS::class, item_supplier_purchase_order);
}
}
class PURCHASE_ORDERS extends Model
{
public function purchaseOrder()
{
return $this->belongsToMany(ItemSupplier::class, item_supplier_purchase_order);
}
}
2 回答
为数据透视表添加模型
ItemSupplier
whth extra columnid
,扩展 Pivot如您所见,您需要创建一个新的
item_supplier_purchase_order
数据透视表并添加关系将
id
列添加到item_supplier
并为数据透视表创建ItemSupplier
模型 . 然后在ItemSupplier
和PurchaseOrder
模型之间创建一个新的item_supplier_purchase_order
数据透视表和两个belongsToMany()
关系 .此外,当从
item_supplier
数据透视表中删除相关记录时,不要忘记使用cascade deleting从新数据透视表中删除记录 .