如何解决如何在模型laravel中声明想要的关系
我有三个表,products,折扣和dicount_products。 折扣表具有折扣数据,而Discount_products具有产品ID和折扣表的ID,以便在它们之间建立连接。
在我的模型产品中,我试图通过这种方式通过关系表获得折扣
//the discount relationship table
public function discount_list(){
return $this->hasOne(Discount_products::class,'product_id','id');
}
//getting the discount throught the discount_list
public function discount(){
return $this->discount_list()->discount();
}
Discount_products模型
public function discount(){
return $this->belongsTo(Discounts::class,'discount_id','id');
}
PS:我知道我可以在控制器中执行此操作,但是我试图一步一步实现折扣。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。