douzi1991
2017-10-23 16:23
浏览 47
已采纳

如何从Laravel 5.5中的3个表中获取所需的行

I have 3 tables, Order, Products, Order_Products. I need get all field from order and products, thats ok using hasManyThrough(). But i need 1 more field from Order_products. How can i get this field ?

public function products()
{
   //in order model
 return $this->hasManyThrough('App\Models\Product','App\Models\OrderProduct','order_id','id','id','product_id');
}

using sql i need query like

SELECT products.*, order_products.order_id, order_products.count as order_count FROM products INNER JOIN order_products ON order_products.product_id = products.id WHERE order_products.order_id = 2

图片转代码服务由CSDN问答提供 功能建议

我有3个表,Order,Products,Order_Products。 我需要从订单和产品中获取所有字段,这可以使用hasManyThrough()。 但我还需要Order_products的另外一个字段。 我怎样才能得到这个字段?

 公共功能产品()
 {
 //在订单模型中
返回$ this-> hasManyThrough('App \ Models  \产品”, '应用程序\模型\ OrderProduct', 'ORDER_ID', 'ID', 'ID', 'PRODUCT_ID'); \ N} 
   
 
 

使用 我需要查询

SELECT products 。*, order_products order_id order_products count as order_count FROM products INNER JOIN order_products ON order_products product_id = products id WHERE order_products order_id = 2

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchu4048 2017-10-23 16:41
    已采纳

    You can access intermediate table fields by using pivot attribute on model.

    Lets say you have product, Then you can access count field of orders_products

    $product->pivot->count;
    
    点赞 打赏 评论

相关推荐 更多相似问题